Data Structures & AlgorithmsHardAmazon/Microsoft SDE pattern
Heights [9, 2, 9, 6, 1, 6, 3] represent vertical lines at each index. What is the maximum water area (min(height) × horizontal distance) obtainable between any two lines?
- A. 28
- B. 18
- C. 6
- D. 30
Correct answer: D. 30
Checking all pairs (equivalently, the two-pointer sweep that moves the shorter boundary inward) finds the best area is 30, between indices 0 and 5. Only using the two outermost lines, 18, is not always optimal since a taller inner pair can win.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [2, 1, 0, 2, 2, 0, 1, 2, 2] (values 0, 1, 2 only)…
- What is the sum of the minimum element over every contiguous subarray of [3, 6, 5, 3, 8, 4, 9, 1]?
- Two sorted linked lists [7 → 13 → 23 → 40] and [5 → 6 → 9 → 18 → 25] are merged with the standard O(n+m) two-pointer merge (always take…
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [2, 0, 2, 2, 1, 0, 1, 1] (values 0, 1, 2 only) in…
- Array [85, 11, 19, 30, 40, 43, 70] is a sorted array rotated at pivot 6. Using the standard O(log n) rotated-array binary search (decide…
- A min-stack (push/pop O(1), getMin O(1) via an auxiliary stack tracking the running minimum) runs this sequence in order: push(-2)…