Values slice is shortened by one element correctly.
Step 2: Check mins slice operation
Mins slice is sliced to full length, so it is not shortened, causing mismatch.
Final Answer:
The mins slice is not shortened on pop -> Option D
Quick Check:
mins slice must shrink with values [OK]
Hint: Pop must shrink both values and mins slices equally [OK]
Common Mistakes:
Forgetting to shrink mins slice
Assuming Pop returns value
Clearing slices fully on pop
5. You want to design a Min Stack that supports Push, Pop, Top, and GetMin all in O(1) time. Which approach below correctly handles the minimum tracking when pushing a new element x?
hard
A. Push x to values; push x only if x < current min to mins
B. Push x to values; push min(x, current min) to mins
C. Push x to values; push current min only if x > current min to mins
D. Push x to values; do not update mins slice
Solution
Step 1: Understand min tracking logic
Each push must record the minimum so far to allow O(1) GetMin.
Step 2: Compare new element with current min
Push the smaller of x and current min onto mins slice to keep track.
Final Answer:
Push x to values; push min(x, current min) to mins -> Option B
Quick Check:
Always push min(x, current min) to mins [OK]
Hint: Always push min of new and current min to mins stack [OK]
Common Mistakes:
Pushing x only if smaller causes missing mins
Not updating mins on every push
Ignoring mins slice updates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.