State after a sequence
Warm-up · Trace the algorithm · about 4 min
The stack holds 5, 2, 8 from bottom to top. Follow the operations pop, pop, push 8, push 2, push 6, peek, pop, and answer each question before the step plays.
Example 1
Input:
{"values":[5,2,8],"ops":["pop","pop","push 8","push 2","push 6","peek","pop"]}
Output:
[5,8,2]
Constraints
- The trace uses the input shown.
Learn the idea first: Stacks: push, pop, peek; array and linked implementations, a visual lesson.
More Stacks: push, pop, peek; array and linked implementations problems
- Overflow handling Warm-up
- Implement a stack on an array Standard
- Baseball game score Standard
- Spot the bug: pop returns the wrong value Standard
- Implement a stack with a linked list Challenge