A Double-ended queue supports operations such as adding and removing items from both the sides of the queue. They support four operations like addFront(adding item to top of the queue), addRear(adding item to the bottom of the queue), removeFront(removing item from the top of the queue) and removeRear(removing item from the bottom of the queue). You are given only stacks to implement this data structure. You can implement only push and pop operations. What are the total number of stacks required for this operation?(you can reuse the stack)
A. 1
B. 2
C. 3
D. 4
Answer:B
A Double-ended queue supports operations such as adding and removing items from both the sides of the queue. They support four operations like addFront(adding item to top of the queue), addRear(adding item to the bottom of the queue), removeFront(removing item from the top of the queue) and removeRear(removing item from the bottom of the queue). You are given only stacks to implement this data structure. You can implement only push and pop operations. What are the total number of stacks required for this operation?(you can reuse the stack)
A. 1 B. 2 C. 3 D. 4
Explanation: Push and pop (adding and removing elements from the top of the stack) are supported in the addFront and removeFront operations, but to perform addRear and removeRear, you must first pop each element from the current stack and push it into another stack, then push or pop the element as per the asked operation from this stack, and finally pop elements from this stack to the top of the stack.