Simulate Water Settling on a Terrain Elevation Profile

Last updated: November 29, 2026

Quick Overview

Given an array of terrain heights, a number of water units, and a drop index, release the units one at a time at that index. Each unit flows left if moving left lets it eventually reach a lower level, otherwise right under the same rule, otherwise it stays put; return the final combined heights.

Airbnb
Coding & Algorithms
Software Engineer
Airbnb
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given an array of terrain heights, a number of water units, and a drop index, release the units one at a time at that index. Each unit flows left if moving left lets it eventually reach a lower level, otherwise right under the same rule, otherwise it stays put; return the final combined heights.

Airbnb is known for coding rounds that are simulations with precise rules rather than textbook algorithms, and this pouring-water style problem is a good example. The interviewer watches whether you nail the tie-breaking rules and write a loop that is easy to reason about.

What the Interviewer Expects
  • Restate the movement rules and edge behavior before coding
  • Simulate one unit at a time rather than trying to be clever
  • Pick the closest lowest spot when there is a flat stretch
  • Stop walking when terrain rises
  • Treat array ends as walls unless told otherwise
Key Topics to Cover
simulation
arrays
two-direction scan
tie-breaking
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you print the terrain and water as an ASCII picture?
  • If water can spill off the ends of the array, what changes?
  • How would you speed this up when volume is in the millions?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Inputs: heights (non-negative ints), volume, and k. A unit at k first looks left: it may travel over cells whose height is not greater than the cell it is on, and it settles at the first strictly lowe...

High-Level Design

For each unit, scan left from k while the next cell is not higher, tracking best, the first cell with a new minimum height. If best moved off k, add one unit there. Otherwise repeat to the right, ...


Submit Your Answer
Markdown supported