Data Structures & AlgorithmsVery hardFlipkart/Uber OA · placement
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, 5, 7, 5, 5, "pop", "pop", 3]:
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, 5, 5, 5, 5, 7, 7, 3]
- B. [7, 5, 5, 5, 5, 5, 5, 3]
- C. [7, 5, 5, 5, 5, 5, 3, 5]
- D. [5, 7, 5, 5, 5, 5, 5, 3]
Correct answer: B. [7, 5, 5, 5, 5, 5, 5, 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, 5, 5, 5, 5, 5, 5, 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
- A hash-map two-sum returns the first index pair (i, j), i < j, found while scanning left to right. What is printed? python def twosum(a…
- A dynamic array doubles its capacity when full, copying all elements. Starting from capacity 1, what is the total number of element copies…
- Evaluate the postfix expression 7 4 1 ÷ 7 + 6 × 2 × + 3 − (all divisions are exact). What is its value, and what is the maximum number of…
- Asteroids move along a line: positive values move right, negative move left; on collision the smaller (by absolute value) explodes, equal…
- One Lomuto partition (pivot = LAST element; i marks the end of the 'less than pivot' region; when a[j] < pivot, increment i and swap a[i]…
- Keys 47, 74, 63, 43, 45, 59, 85 are inserted in this order into an empty table of size 13 using quadratic probing: probe i goes to (k mod…