A queue is implemented with the help of two stacks. 5 characters a, b, c, d, e are inserted in the queue and after these successful insertions, a and b are deleted from the queue. How many total operation in Push() and Pop() are required?
(A) 7 Push() and 7 Pop() are required
(B) 10 Push() and 7 Pop() are required
(C) 7 Push() and 10 Pop() are required
(D) 10 Push() and 10 Pop() are required


Answer: (B)

Explanation: We have two stacks to implement a queue:
Firstly all 5 characters are pushed into the stack = (5 push).
Now we have to deque a, b. For this operation pop all 5 elements and push it into another stack in reverse order i.e. e, d, c, b, a.
To do this pop from first stack and push it into second stack = (5 pop + 5 push)
Now pop a, b from the second stack (following the property of queue first in first out) = (2 pop)

Therefore, total 10 Push() and 7 Pop() are required.

Option (B) is correct.

Quiz of this Question


  • Last Updated : 25 Dec, 2018

Share your thoughts in the comments