Data Structures & AlgorithmsModerate
A min-stack supports push, pop and getMin in O(1). Apply this sequence, starting empty: push 5, push 3, push 7, getMin, pop, push 1, getMin, push 0, getMin, pop, pop, getMin. List the values returned by all the getMin calls, in order (four numbers).
- A. 3 1 0 0
- B. 3 1 0
- C. 3 1 0 3
- D. 5 3 1 0
Correct answer: C. 3 1 0 3
Each stack entry stores the minimum of everything at or below it, so popping restores the previous minimum. Tracking the all-time minimum ignores pops, and the answer changes once 0 is pushed and then removed.
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
- Asteroids in a row have sizes given by 5 8 -3 -8 10 -4 2. Positive values move right, negative values move left; two meeting asteroids…
- People stand in a row with heights [10, 6, 8, 5, 11, 9]. Person i can see person j > i if every person strictly between them is shorter…
- In the unsorted array 100 4 200 1 3 2 4 101 102 103 3, what is the length of the longest run of CONSECUTIVE integers (values like 5, 6, 7…
- For the array 1 2 3 4, how many DISTINCT values appear as the XOR of a non-empty contiguous subarray?
- Starting at index 0 of 2 3 1 1 4 2 3 1 1 4, a value a[i] lets you jump from index i to any index in i+1 … i+a[i]. What is the minimum…
- Remove duplicate values from the unsorted list 4 2 4 1 2 5 1 so that only ONE copy of each value remains, keeping the copy that appears…