Bird
Raised Fist0
Interview Prepchallenge-problemshardAmazonGoogleFacebookMicrosoftBloomberg

Trapping Rain Water

Choose your preparation mode4 modes available
▶
Steps
setup

Initialize pointers and variables

Set left pointer at index 0 and right pointer at index 11 (end of array). Initialize left_max, right_max, and water to 0.

💡 Starting pointers at both ends allows scanning inward to find trapped water efficiently.
Line:left, right = 0, len(height) - 1 left_max, right_max = 0, 0 water = 0
💡 We prepare to compare heights from both ends to decide which pointer to move.
📊
Trapping Rain Water - Watch the Algorithm Execute, Step by Step
Watching the pointers move and how trapped water accumulates visually reveals the logic behind the two-pointer approach, making the algorithm intuitive without needing to parse code alone.
Step 1/11
·Active fill★Answer cell
setup
left
0
0
1
1
0
2
2
3
1
4
0
5
1
6
3
7
2
8
1
9
2
10
right
1
11
Result: 0
compare
left
0
0
1
1
0
2
2
3
1
4
0
5
1
6
3
7
2
8
1
9
2
10
right
1
11
Result: 0
shrink
left
0
0
1
1
0
2
2
3
1
4
0
5
1
6
3
7
2
8
1
9
2
10
right
1
11
Result: 0
move_left
0
0
left
1
1
0
2
2
3
1
4
0
5
1
6
3
7
2
8
1
9
2
10
right
1
11
Result: 0
compare
0
0
left
1
1
0
2
2
3
1
4
0
5
1
6
3
7
2
8
1
9
2
10
right
1
11
Result: 0
shrink
0
0
left
1
1
0
2
2
3
1
4
0
5
1
6
3
7
2
8
1
9
2
10
right
1
11
Result: 0
move_right
0
0
left
1
1
0
2
2
3
1
4
0
5
1
6
3
7
2
8
1
9
right
2
10
1
11
Result: 0
record
0
0
1
1
0
2
2
3
1
4
0
5
1
6
3
7
2
8
left
1
9
right
2
10
1
11
Result: 6
compare
0
0
1
1
0
2
2
3
1
4
0
5
1
6
3
7
2
8
left
1
9
right
2
10
1
11
Result: 6
move_left
0
0
1
1
0
2
2
3
1
4
0
5
1
6
3
7
2
8
1
9
right
2
10
1
11
Result: 6
reconstruct
0
0
1
1
0
2
2
3
1
4
0
5
1
6
3
7
2
8
1
9
right
2
10
1
11
Result: 6

Key Takeaways

✓ The two-pointer approach efficiently calculates trapped water by moving inward from both ends and using max boundaries.

This insight is hard to see from code alone because it requires understanding why the smaller height pointer moves and how max boundaries limit trapped water.

✓ Trapped water at each position depends on the difference between the current height and the max height seen so far on that side.

Visualizing the max heights and pointer movements clarifies how water accumulates only where bars are lower than these boundaries.

✓ Pointer movement decisions are based on comparing heights at pointers, ensuring the algorithm processes the limiting side first.

The trace shows concrete examples of when and why the left or right pointer moves, which is subtle in code but clear visually.

Practice

(1/5)
1. Consider the following Python code implementing the optimal Alien Dictionary solution. Given the input words = ["wrt", "wrf", "er", "ett", "rftt"], what is the final output string returned by the function?
easy
A. "wrtef"
B. "wert"
C. "wertf"
D. "wretf"

Solution

  1. Step 1: Build graph edges from adjacent words

    Edges: w->e (from "wrt" and "wrf"), e->r ("wrf" and "er"), r->t ("er" and "ett"), t->f ("ett" and "rftt")
  2. Step 2: Perform topological sort using Kahn's algorithm

    Start with nodes with zero in-degree: w; order: w -> e -> r -> t -> f
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Output "wertf" matches topological order from graph [OK]
Hint: Trace edges and zero in-degree queue carefully [OK]
Common Mistakes:
  • Off-by-one in queue processing
  • Ignoring some edges
  • Returning partial order
2. You are given an array of balloons, each with a number representing coins. When you burst a balloon, you gain coins equal to the product of the balloon's number and its adjacent balloons' numbers. After bursting, the balloon disappears, and the neighbors become adjacent. Which algorithmic approach guarantees finding the maximum coins you can collect by bursting all balloons in an optimal order?
easy
A. A greedy algorithm that always bursts the balloon with the smallest number first
B. Sorting balloons and bursting them in ascending order to maximize adjacent products
C. A simple depth-first search without memoization to explore all bursting sequences
D. Dynamic programming that considers all subproblems of bursting balloons between indices and uses bottom-up tabulation

Solution

  1. Step 1: Understand problem constraints

    The problem requires maximizing coins by choosing the order of bursting balloons, where each burst depends on neighbors that change dynamically.
  2. Step 2: Identify algorithmic pattern

    Greedy or sorting approaches fail because the optimal choice depends on future bursts. DFS without memoization is exponential and inefficient. Dynamic programming with bottom-up tabulation systematically solves overlapping subproblems and ensures optimality.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    DP solves all subproblems optimally [OK]
Hint: Optimal order depends on future bursts, so DP is needed [OK]
Common Mistakes:
  • Assuming greedy bursting works
  • Ignoring overlapping subproblems
  • Not using memoization
3. What is the time complexity of the optimized sliding window algorithm for the Minimum Window Substring problem, assuming the input strings have lengths n and m respectively, and the character set is fixed ASCII (size 128)?
medium
A. O(n * m) because for each character in s, we check all characters in t
B. O(n) because each character in s is visited at most twice by the two pointers
C. O(n log n) due to sorting or balanced tree operations on window counts
D. O(n + m) because we initialize frequency arrays and scan s once

Solution

  1. Step 1: Analyze outer and inner loops

    The right pointer moves from 0 to n-1 once, and the left pointer moves forward only when conditions are met, also at most n times.
  2. Step 2: Confirm linear time complexity

    Each character is processed at most twice (once when right pointer moves forward, once when left pointer moves forward), so total time is O(n). Frequency arrays are fixed size (128), so operations are O(1).
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Two-pointer sliding window with fixed frequency arrays is O(n) time [OK]
Hint: Two pointers each traverse string once -> O(n) time [OK]
Common Mistakes:
  • Confusing with brute force O(n*m)
  • Assuming sorting or complex data structures involved
4. What is the time complexity of the optimal algorithm that sorts envelopes by width ascending and height descending, then finds the Longest Increasing Subsequence (LIS) on heights using binary search?
medium
A. O(n) since LIS can be done in linear time after sorting
B. O(n²) due to nested loops in LIS calculation
C. O(n log n) for sorting plus O(n²) for LIS, total O(n²)
D. O(n log n) because sorting is O(n log n) and LIS with binary search is O(n log n)

Solution

  1. Step 1: Analyze sorting complexity

    Sorting envelopes by width and height takes O(n log n).
  2. Step 2: Analyze LIS with binary search

    LIS with binary search runs in O(n log n) time.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Overall complexity is dominated by O(n log n) sorting and LIS [OK]
Hint: Sorting + LIS with binary search -> O(n log n) [OK]
Common Mistakes:
  • Assuming LIS is always O(n²)
  • Ignoring binary search optimization in LIS
  • Confusing sorting and LIS complexities
5. What is the time complexity of the divide and conquer approach to the Skyline Problem when given n buildings, and why might a common misconception about the complexity be incorrect?
medium
A. O(n log n) because the problem is divided into halves and merged efficiently
B. O(n log n) time but O(n²) space due to storing all intermediate skylines
C. O(n log n) but with additional O(n) space overhead for recursion stack making it O(n log n) space
D. O(n²) because each merge step compares all points in both skylines

Solution

  1. Step 1: Identify divide and conquer structure

    The buildings are split into halves recursively, leading to log n levels.
  2. Step 2: Analyze merge cost per level

    Each merge step processes all points in the two skylines combined, which sums to O(n) per level.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Divide and conquer with linear merge per level -> O(n log n) time [OK]
Hint: Divide and conquer merges cost O(n) per level over log n levels [OK]
Common Mistakes:
  • Assuming merge is O(n²) due to nested loops
  • Confusing recursion stack space with time complexity
  • Thinking space grows quadratically due to intermediate skylines
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard