Bird
0
0
DSA Cprogramming~10 mins

Min Stack Design in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Min Stack Design
Push x
Push x to main stack
Is min stack empty or x <= top of min stack?
NoDo nothing
Yes
Push x to min stack
Done
Pop
Pop from main stack
Is popped value == top of min stack?
NoDo nothing
Yes
Pop from min stack
Done
GetMin
Return top of min stack
Push adds to main stack and min stack if needed; Pop removes from both if top matches; GetMin returns min stack top.
Execution Sample
DSA C
push(5)
push(3)
push(7)
pop()
getMin()
Push values 5, 3, 7; pop top; get minimum value in stack.
Execution Table
StepOperationMain StackMin StackPointer ChangesVisual State
1push(5)55main_top=0, min_top=0Main: 5 -> null; Min: 5 -> null
2push(3)5 -> 35 -> 3main_top=1, min_top=1Main: 5 -> 3 -> null; Min: 5 -> 3 -> null
3push(7)5 -> 3 -> 75 -> 3main_top=2, min_top=1Main: 5 -> 3 -> 7 -> null; Min: 5 -> 3 -> null
4pop()5 -> 35 -> 3main_top=1, min_top=1Main: 5 -> 3 -> null; Min: 5 -> 3 -> null
5getMin()5 -> 35 -> 3No changeMin top is 3
💡 Execution stops after getMin returns current minimum 3.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
main_stackempty[5][5,3][5,3,7][5,3][5,3]
min_stackempty[5][5,3][5,3][5,3][5,3]
main_top-101211
min_top-101111
Key Moments - 3 Insights
Why do we push to min stack only when the new value is less or equal to the current min?
Because min stack keeps track of minimums. See step 3 in execution_table: 7 is not pushed to min stack since 7 > current min 3.
What happens to min stack when we pop a value that is not the current minimum?
Min stack stays unchanged. See step 4: popped 7 from main stack but min stack remains same.
How does getMin() return the minimum value efficiently?
It returns the top of min stack instantly without scanning main stack, as shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the state of the min stack?
A[5, 3, 7]
B[5, 3]
C[3]
D[7]
💡 Hint
Check the 'Min Stack' column at step 3 in execution_table.
At which step does the main stack size decrease?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at 'Main Stack' column changes in execution_table.
If we push(2) after step 4, what happens to min stack?
AMin stack pops top
BMin stack remains unchanged
C2 is pushed to min stack
DMin stack resets
💡 Hint
Recall min stack pushes new value if it is <= current min (see concept_flow).
Concept Snapshot
Min Stack Design:
- Use two stacks: main and min.
- Push: add to main; if new <= min top, push to min.
- Pop: remove from main; if popped == min top, pop min.
- getMin: return top of min stack instantly.
- Keeps track of minimum in O(1) time.
Full Transcript
Min Stack uses two stacks to track values and minimums. When pushing, the value goes to main stack. If it is smaller or equal to current minimum, it also goes to min stack. When popping, if the popped value equals min stack top, pop min stack too. getMin returns the top of min stack, giving minimum in constant time. This design ensures efficient minimum retrieval while maintaining stack behavior.