Implementing Queue with Two Stacks
Your task is to implement a queue using two stacks. You are provided with ‘Q’ queries and need to handle them, where each query falls under one of these two operations:
- Enqueue an element ‘X’ to the end of the queue.
- Dequeue an element from the front of the queue.
Functionality:
- For enqueue, return true after the element is enqueued.
- For dequeue, return -1 if the queue is empty, otherwise return the dequeued element.
Example:
Input:
5
1 10
1 20
2
1 30
2
Output:
true
true
10
true
20
Explanation:
The given queries perform the following operations:
- Enqueue 10: Returns true
- Enqueue 20: Returns true
- Dequeue: Returns 10 (dequeued)
- Enqueue 30: Returns true
- Dequeue: Returns 20 (dequeued)
Constraints:
1 <= Q <= 10^5
1 <= P <= 2
1 <= X <= 10^5
(for enqueue operation)- Time limit: 1 sec

AnswerBot
4mo
Implement a queue using two stacks with enqueue and dequeue operations.
Use two stacks to simulate a queue - one for enqueue and one for dequeue.
For enqueue operation, push elements onto the enqueue st...read more
Help your peers!
Add answer anonymously...
Springworks Software Developer Intern interview questions & answers
A Software Developer Intern was asked Q. Linear Probing in Hashing Hashing is a technique to map large non-negative integ...read more
A Software Developer Intern was asked Q. Roman Numeral to Integer Conversion Convert a string representing a Roman numera...read more
A Software Developer Intern was asked Q. Pair Sum Problem Statement You are given an array of integers 'ARR' with a lengt...read more
Popular interview questions of Software Developer Intern
A Software Developer Intern was asked Q1. Linear Probing in Hashing Hashing is a technique to map large non-negative integ...read more
A Software Developer Intern was asked Q2. Roman Numeral to Integer Conversion Convert a string representing a Roman numera...read more
A Software Developer Intern was asked Q3. Pair Sum Problem Statement You are given an array of integers 'ARR' with a lengt...read more
>
Springworks Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

