ISRO Scientist/Engineer CSE 2017Data Structures › Stacks and Expression Evaluation
ISRO Scientist/Engineer CSE 2017 PYQ: The minimum number of stacks needed to implement a queue is
The minimum number of stacks needed to implement a queue is
- A3
- B1
- C2Correct answer
- D4
Answer
(C) $2$
Approach
A queue is FIFO while a stack is LIFO. To reverse the order, we need one stack to hold elements in original order and a second stack to reverse them during dequeue. Hence two stacks suffice, and one stack cannot implement a queue because a single stack cannot reverse order without extra storage.
Reviewed by ISRO IQ Editorial Team.