Data Structures & AlgorithmsVery hardTCS NQT · Infosys 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(-2), push(-1), push(-4), push(-8), pop(), push(-8), push(6), push(1), getMin(). What does the final getMin() return?
- A. -7
- B. 6
- 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
- Array [1, 2, 3, 4, 5, 6, 7, 8, 9] is left-rotated by d = 6 positions using the reversal algorithm (reverse first d, reverse rest, reverse…
- An LRU cache of capacity 3 runs this sequence of operations in order: put(4, 40), put(2, 20), put(5, 50), put(1, 10), put(1, 10), put(2…
- What is the postfix (Reverse Polish) form of the infix expression "(i + j) d - b / h", built with the standard stack-based conversion (^…
- Array [61, 75, 77, 8, 27, 29, 31, 32, 47, 56] is a sorted array rotated at pivot 7. Using the standard O(log n) rotated-array binary…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- Elevation map [2, 2, 5, 0, 2, 0, 3, 1, 1, 1] (bar width 1). Using the two-pointer / prefix-max approach for trapping rain water, how much…