Data Structures & AlgorithmsVery hardAmazon/Microsoft SDE pattern
A min-stack (push/pop O(1), getMin O(1) via an auxiliary stack tracking the running minimum) runs this sequence in order: push(-3), push(0), pop(), pop(), push(8), push(-7), push(1), push(0), push(-2), getMin(). What does the final getMin() return?
- A. -2
- B. 8
- C. -7
- D. -6
Correct answer: C. -7
The auxiliary stack pushes min(new value, current min) on every push and pops in lockstep with the main stack, so its top always holds the minimum of the elements currently on the stack. Tracing all operations in order leaves the minimum as -7; -2 is just the top of the main stack, not the minimum.
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
- Heights [3, 6, 1, 4, 7, 9, 3, 7, 7, 4] represent vertical lines at each index. What is the maximum water area (min(height) × horizontal…
- Binary search for first/last occurrence runs on the sorted array [8, 9, 15, 18, 18, 20, 21, 22] for target 18. What (first index, last…
- What is the sum of the minimum element over every contiguous subarray of [4, 1, 1, 2, 3, 3, 7, 3]?
- What does this loop-counting code print? (Python) python n, c = 42, 0 i = 1 while i < n: j = i while j < n: c += 1 j = 2 i += 1 print(c)
- A circular queue of capacity 5 starts empty and runs this sequence in order: enqueue(6), dequeue(), enqueue(4), enqueue(6), dequeue()…
- The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [9, 6, 6, 6, 6, 5, 6]. What candidate and final…