If the sequence of operations - push (1), push (2), pop, push (1), push (2),…
2015
If the sequence of operations - push (1), push (2), pop, push (1), push (2), pop, pop, pop, push (2), pop are performed on a stack, the sequence of popped out values
- A.
2,2,1,1,2
- B.
2,2,1,2,2
- C.
2,1,2,2,1
- D.
2,1,2,2,2
Attempted by 616 students.
Show answer & explanation
Correct answer: A
Simulate the stack operations step-by-step to track pushed and popped values.
Push 1, Push 2: Stack is [1, 2]. Pop returns 2. Stack becomes [1].
Push 1, Push 2: Stack is [1, 1, 2]. Pop returns 2. Stack becomes [1, 1].
Pop returns 1. Stack becomes [1]. Pop returns 1. Stack is empty.
Push 2, Pop returns 2.
The sequence of popped values is 2, 2, 1, 1, 2.
A video solution is available for this question — log in and enroll to watch it.