← Questions
Pour water onto a terrain
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Airbnb's technical phone screen, last asked Feb 2026. It tests simulation and arrays.
An array heights gives the height of each column of a terrain, one unit wide. First print the terrain. Then drop units of water, one unit at a time, at column k, and print the terrain with the water.
Part 1: print the terrain
-
render(heights, water) -> list[str]: one string per row from the top down;+for terrain,Wfor water, a space for air.
Part 2: pour the water
Unless you agree on a different interface with the interviewer, assume these rules for each unit. The level of a column is its height plus the water on it.
- Move left while the next column's level is not higher than the current one. If that walk passed a column lower than column
k, the unit settles at the first lowest one it reached. - Otherwise try the same to the right.
- Otherwise it stays at
k.
-
pour(heights, units, k) -> list[int]: the water on each column. Water never flows off the edges.
Example
heights = [3, 1, 2, 0, 1, 3], k = 2
pour(heights, 1, 2) -> [0, 1, 0, 0, 0, 0] # falls left into column 1
pour(heights, 2, 2) -> [0, 1, 0, 1, 0, 0] # left is now level; goes right to column 3
pour(heights, 5, 2) -> [0, 1, 1, 2, 1, 0]
terrain: after 5 units:
+ + + W +
+ + + +W+WW+
+++ ++ +++W++
Constraints
- Up to 100 columns, heights up to 100, up to 2,000 units.
-
0 <= k < len(heights).
Clarifying questions
- Exactly how does a unit choose between left and right?
- What happens at the edges: walls, or water falls off?
- Should printing show water above the terrain only, or also trapped pockets?
What a strong answer covers
- Agreeing the movement rule precisely before coding, with a small trace.
- Simulation: O(units × columns), fine for these sizes.
- Rendering kept separate from the simulation, computed from levels row by row.
- Tests: a flat terrain, water filling a valley, drops at an edge, many units overflowing into neighbors.
Follow-ups
- Water falls off the edges.
- Pour from several positions.
- A much larger number of units: fill whole levels at once instead of one unit at a time.
Where it's asked
- Airbnb · Technical phone screen · Last asked Feb 202645 min · One algorithm problem from a small recurring pool
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
