Bird
Raised Fist0
Interview Preptwo-pointersmediumAmazonGoogleFacebookBloomberg

Container With Most Water

Choose your preparation mode4 modes available
▶
Steps
setup

Initialize pointers and calculate initial area

Set left pointer at index 0 and right pointer at index 8 (ends of the array). Calculate the initial area using these pointers and set max_area.

💡 Starting with the widest container gives a baseline max area to compare against as pointers move inward.
Line:left, right = 0, len(height) - 1 max_area = (right - left) * min(height[left], height[right])
💡 Initial max_area is based on the widest container formed by the outermost lines.
📊
Container With Most Water - Watch the Algorithm Execute, Step by Step
Watching the pointers move and areas update visually helps you understand why moving the shorter line pointer is optimal and how the maximum area is found efficiently.
Step 1/17
·Active fill★Answer cell
compare
left
1
0
8
1
6
2
2
3
5
4
4
5
8
6
3
7
right
7
8
Result: 7
move_left
1
0
left
8
1
6
2
2
3
5
4
4
5
8
6
3
7
right
7
8
Result: 7
record
1
0
left
8
1
6
2
2
3
5
4
4
5
8
6
3
7
right
7
8
Result: 49
move_right
1
0
left
8
1
6
2
2
3
5
4
4
5
8
6
right
3
7
7
8
Result: 49
record
1
0
left
8
1
6
2
2
3
5
4
4
5
8
6
right
3
7
7
8
Result: 49
move_right
1
0
left
8
1
6
2
2
3
5
4
4
5
right
8
6
3
7
7
8
Result: 49
record
1
0
left
8
1
6
2
2
3
5
4
4
5
right
8
6
3
7
7
8
Result: 49
move_right
1
0
left
8
1
6
2
2
3
5
4
right
4
5
8
6
3
7
7
8
Result: 49
record
1
0
left
8
1
6
2
2
3
5
4
right
4
5
8
6
3
7
7
8
Result: 49
move_right
1
0
left
8
1
6
2
2
3
right
5
4
4
5
8
6
3
7
7
8
Result: 49
record
1
0
left
8
1
6
2
2
3
right
5
4
4
5
8
6
3
7
7
8
Result: 49
move_right
1
0
left
8
1
6
2
right
2
3
5
4
4
5
8
6
3
7
7
8
Result: 49
record
1
0
left
8
1
6
2
right
2
3
5
4
4
5
8
6
3
7
7
8
Result: 49
move_right
1
0
left
8
1
right
6
2
2
3
5
4
4
5
8
6
3
7
7
8
Result: 49
record
1
0
left
8
1
right
6
2
2
3
5
4
4
5
8
6
3
7
7
8
Result: 49
move_right
1
0
right
8
1
6
2
2
3
5
4
4
5
8
6
3
7
7
8
Result: 49
prune
1
0
right
8
1
6
2
2
3
5
4
4
5
8
6
3
7
7
8
Result: 49

Key Takeaways

✓ Moving the pointer at the shorter line is the key to efficiently finding the maximum area.

This insight is hard to see from code alone because it requires understanding why moving the shorter line can lead to a larger area, which is clearer when watching pointer movements.

✓ The maximum area is not always formed by the widest container but by a balance of width and height.

Visualizing the area calculations at each step helps understand how height and width trade off to maximize area.

✓ The algorithm terminates when pointers meet, ensuring all relevant pairs are considered without redundant checks.

Seeing pointers converge step-by-step clarifies the loop termination condition and why no pairs are missed.

Practice

(1/5)
1. Consider the following Python code implementing the optimal two-pointer forward approach for backspace string compare. What is the value of the variable write after processing the input string S = "ab#c" inside the build function?
easy
A. 2
B. 3
C. 4
D. 1

Solution

  1. Step 1: Trace the build function with input "ab#c"

    Start with write=0. Read 'a' -> arr[0]='a', write=1. Read 'b' -> arr[1]='b', write=2. Read '#' -> write=1 (backspace removes 'b'). Read 'c' -> arr[1]='c', write=2.
  2. Step 2: Final value of write

    After processing, write=2, indicating the final string length is 2 ("ac").
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    write increments and decrements correctly for backspace [OK]
Hint: write increments on char, decrements on backspace if possible [OK]
Common Mistakes:
  • Counting '#' as a character and incrementing write
  • Not decrementing write on backspace
  • Off-by-one errors in write pointer
2. Consider the following Python code for squaring and sorting an array in-place:
def sortedSquares(nums):
    for i in range(len(nums)):
        nums[i] = nums[i] * nums[i]
    nums.sort()
    return nums

nums = [-3, 0, 2]
print(sortedSquares(nums))
What is the output of this code?
easy
A. [9, 4, 0]
B. [9, 0, 4]
C. [0, 4, 9]
D. [4, 0, 9]

Solution

  1. Step 1: Square each element

    nums becomes [9, 0, 4] after squaring -3, 0, and 2 respectively.
  2. Step 2: Sort the squared array

    Sorting [9, 0, 4] results in [0, 4, 9].
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Squares then sort yields sorted squares [OK]
Hint: Square then sort yields sorted squares [OK]
Common Mistakes:
  • Forgetting to sort after squaring
  • Mixing up order of squared elements
  • Off-by-one errors in indexing
3. You are given a sorted array of integers and a target sum. You need to find two distinct elements whose sum equals the target. Which approach guarantees an optimal solution in terms of time complexity and correctness?
easy
A. Use two pointers starting at opposite ends of the array, moving inward based on the sum comparison.
B. Use a hash map to store elements and check complements in a single pass.
C. Use dynamic programming to store sums of all pairs and check for the target.
D. Use a greedy approach by iterating from the start and picking the first pair that sums to the target.

Solution

  1. Step 1: Understand the problem constraints

    The array is sorted, so two pointers at opposite ends can efficiently find pairs by comparing sums.
  2. Step 2: Evaluate each approach

    Greedy fails because it may miss pairs; DP is unnecessary overhead; hash map works but uses extra space and is not optimal for sorted arrays.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Two pointers exploit sorting for O(n) time and O(1) space [OK]
Hint: Sorted array + two pointers -> optimal pair search [OK]
Common Mistakes:
  • Assuming greedy always works
  • Using DP unnecessarily
  • Ignoring sorted property
4. The following code attempts to move zeroes to the end of the array in-place:
def moveZeroes(nums):
    lastNonZeroFoundAt = 0
    for num in nums:
        if num != 0:
            nums[lastNonZeroFoundAt] = num
            lastNonZeroFoundAt += 1
    # Bug introduced here
    for i in range(lastNonZeroFoundAt, len(nums)):
        nums[i] = nums[i]
What is the bug in this code?
medium
A. The variable lastNonZeroFoundAt is incremented incorrectly.
B. The first loop does not correctly identify non-zero elements.
C. The second loop does not fill zeroes after lastNonZeroFoundAt, leaving old values intact.
D. The assignment inside the first loop overwrites zeroes incorrectly.

Solution

  1. Step 1: Analyze second loop behavior

    The second loop assigns nums[i] = nums[i], which does nothing and fails to fill zeroes.
  2. Step 2: Identify correct fix

    It should assign nums[i] = 0 to move zeroes to the end properly.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Zeroes remain unfilled, causing incorrect output [OK]
Hint: Second loop must fill zeroes, not copy existing values [OK]
Common Mistakes:
  • Forgetting to fill zeroes after moving non-zero elements
  • Misunderstanding loop purpose
5. If the problem changes so that the same element can be used multiple times to reach the target sum (i.e., elements can be reused), which approach correctly solves the problem?
hard
A. Use the two pointers approach moving inward, but allow pointers to stay if sum < target to reuse elements.
B. Use a modified binary search that allows searching from the current index (not i+1) to reuse elements.
C. Use a dynamic programming approach to find combinations allowing repeated elements.
D. Use the brute force nested loops but break early when sum exceeds target.

Solution

  1. Step 1: Understand reuse requirement

    Allowing reuse means pairs can include the same element multiple times, which breaks two-pointer and binary search assumptions.
  2. Step 2: Identify suitable approach

    Dynamic programming can handle combinations with repeated elements efficiently, unlike two pointers or binary search.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    DP handles repeated elements and target sums correctly [OK]
Hint: Reuse -> DP for combinations, not two pointers or binary search [OK]
Common Mistakes:
  • Trying to reuse pointers incorrectly
  • Modifying binary search without correctness
  • Assuming brute force is efficient
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