Michael Kozakov is a computer science student student at the University of Toronto, and he has also interned at Amazon. But a few weeks ago, he also applied to work at Twitter.

Luckily for us, he wrote a blog post about the experience. (At one point, he was asked, “is this an anagram of a palindrome?”)

But because he was being considered for a coding position, Kozakov was asked to create a model for solving a maths problem, in the form of code.

The problem at first glance seems incredibly simple. But it really separates the liberal arts students from the comp-sci nerds.

We’ve tried to simplify it as much as possible.

Look at the following picture, which represents a bunch of walls of different heights:

Kozakov says he was then asked, “Now imagine it rains. How much water is going to be accumulated in puddles between walls?” (SPOILER: The correct answer is at the very end of this blog post.)

Kozakov’s solution involved writing a piece of code that would detect the maximum heights of each wall, and then filling in the water volume that would fall between them. It looks like this, obviously:

But the interviewer at Twitter said he had not figured out the simplest solution to the problem, because it required two “passes” through the information: Finding the maximum height of each wall, and then calculating the volume between each wall.

In fact, there is a solution that requires only one “pass” through the problem. Imagine the walls aren’t arranged in a convenient bucket formation, but more randomly, like this:

Simply finding the volumes between the *local* maximum wall heights gives you something that looks like this:

But the correct result should be one puddle between the two tallest towers:

Here’s the most elegant solution, from Kozakov’s blog:

The solution in one pass … avoids finding the maximum value by moving two pointers from the opposite ends of the array towards each other. If the largest value found

to the left of the left pointerissmallerthan the largest value foundto the right of the right pointer, then move the left pointer one index to the right. Otherwise move the right pointer one index to the left. Repeat until the two pointers intersect. (This is a wordy explanation, but the code is really simple.)

If you’re a coding simpleton, as we are at Business Insider, these instructions are easier to understand if you hold two fingers up as pointers while you’re reading them. In plain English (as we understand it) the solution is basically to find the two tallest towers that are furthest left and furthest right of the diagram, and then calculate the volume between them.