Simulate Rain Droplets Settling on a One-Dimensional Terrain Profile

Last updated: November 29, 2026

Quick Overview

A terrain profile is given as column heights. V droplets of water fall one at a time at index K; each droplet moves left if it would eventually reach a lower level that way, otherwise right under the same rule, otherwise stays at K, and it settles at the lowest reachable point closest to K. Return the heights after all droplets settle.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


A terrain profile is given as column heights. V droplets of water fall one at a time at index K; each droplet moves left if it would eventually reach a lower level that way, otherwise right under the same rule, otherwise stays at K, and it settles at the lowest reachable point closest to K. Return the heights after all droplets settle.

A Google technical screen simulation with a terrain theme that fits Maps elevation data. The interviewer checks that you translate a wordy rule into precise loop conditions, especially the left-first preference and choosing the nearest lowest point.

What the Interviewer Expects
  • Restate the rule precisely, including the left-before-right preference
  • Scan left while the next column is not higher, tracking the first strictly lower point
  • Only commit left if it is strictly lower than K, otherwise try right
  • Increment the settling column by one per droplet
  • State O(V * n) time
Key Topics to Cover
simulation
arrays
greedy scanning
physical modeling
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 make each droplet faster than O(n) when V is large?
  • What if water can flow off the edges of the profile?
  • How would you return the water depth per column instead of total height?
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

Input: list heights, integers V (droplets) and K (drop index). The terrain is walled beyond both ends. Water units stack like terrain once settled. For each droplet: scanning left from K over no...

High-Level Design

Each droplet is simulated independently on the updated heights. Left scan: i = K, best = K; while i > 0 and h[i-1] <= h[i], step left and if h[i] < h[best] set best = i. Using strict < kee...


Submit Your Answer
Markdown supported