Data Structures & AlgorithmsVery hardProduct placement · Amazon/Microsoft
A min-stack keeps a second stack of running minimums. What is printed?
A min-stack keeps a second stack of running minimums. What is printed?
st, mn, out = [], [], []
for op in [7, 7, "pop", 3, "pop", 3, 3, "pop"]:
if op == "pop":
x = st.pop()
if x == mn[-1]: mn.pop()
else:
st.append(op)
if not mn or op <= mn[-1]: mn.append(op)
out.append(mn[-1] if mn else None)
print(out)- A. [7, 7, 7, 3, 7, 3, 3, 3]
- B. [7, 7, None, 3, None, 3, 3, None]
- C. [7, 7, 3, 7, 7, 3, 3, 3]
- D. [7, 7, 7, 3, 3, 7, 3, 3]
Correct answer: A. [7, 7, 7, 3, 7, 3, 3, 3]
A value is pushed onto the min-stack when it is ≤ the current minimum; using < breaks when a duplicate minimum is popped. Output: [7, 7, 7, 3, 7, 3, 3, 3].
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
- An array of 8 zeros (indices 0–7) receives range updates with a difference array (diff[l] += v, diff[r + 1] −= v, then prefix-sum): add 5…
- Count pairs with a given difference k (k > 0) among DISTINCT values using a hash set. What is printed? python a, k = [2, 11, 6, 2, 5, 9…
- LSD radix sort (base 10, stable sort on each digit, units digit first) is applied to [134, 461, 324, 794, 742, 141, 668]. What is the list…
- What is the length of the smallest substring of "baddcacdabcbd" that contains every distinct character of the string at least once?…
- A circular queue in an array of size 7 keeps one slot empty to tell full from empty (front = rear means empty; (rear + 1) mod 7 = front…
- A fixed sliding window of length 5 moves over "programmingisfun". What is the maximum number of vowels (a, e, i, o, u) inside any window?