Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard
A min-stack (push/pop O(1), getMin O(1) via an auxiliary stack tracking the running minimum) runs this sequence in order: push(-4), pop(), push(9), push(-8), push(-1), push(5), pop(), push(-1), getMin(). What does the final getMin() return?
- A. 9
- B. -7
- C. -8
- D. -1
Correct answer: C. -8
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 -8; -1 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
- Binary search for first/last occurrence runs on the sorted array [1, 5, 6, 7, 10, 13, 18, 19, 19, 20, 21] for target 19. What (first…
- Using a sliding window of fixed size with a character-frequency count, how many substrings of "CBCAACACBABCB" are anagrams of "CAB"?
- The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [1, 3, 3, 1, 1, 3, 3, 3]. What candidate and…
- Array [90, 6, 26, 27, 39, 47, 79] is a sorted array rotated at pivot 6. Using the standard O(log n) rotated-array binary search (decide…
- Two sorted linked lists [12 → 17 → 24 → 26 → 32] and [8 → 10 → 12 → 38] 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 [1, 1, 2, 2, 1, 1, 1, 1, 2, 0] (values 0, 1, 2…