Queue Using Stacks Implementation

Design a queue data structure following the FIFO (First In First Out) principle using only stack instances.

Explanation:

Your task is to complete predefined functions to support all standard queue operations efficiently using stacks.

The queue should handle the following operations:

  1. enQueue(data): Insert an integer 'data' at the end of the queue.
  2. deQueue(): Remove and return the integer from the front of the queue. Return -1 if the queue is empty.
  3. peek(): Return the integer at the front without removing it. Return -1 if the queue is empty.
  4. isEmpty(): Return true if the queue is empty, otherwise false.
Input:
The first line contains an integer 'T', the number of queries.
Each of the next 'T' lines contains a query:
Type '1 val' to insert 'val' into the queue.
Type '2' to remove and return the front element.
Type '3' to return the front element.
Type '4' to check if the queue is empty.
Output:
There is no output needed for query type 1.
For query type 2, return the removed element from the queue.
For query type 3, return the front element from the queue.
For query type 4, return ‘true’ or ‘false’ based on queue's emptiness.
Example:

Suppose the input is:

T = 5 queries.
1 5 (enQueue 5)
1 10 (enQueue 10)
3 (peek the front element)
2 (deQueue the front element)
4 (check if empty)
Corresponding Output:
5 (from peek)
5 (from deQueue)
false (queue is not empty)
Constraints:
  • 1 <= T <= 1000
  • 1 <= type <= 4
  • 1 <= data <= 10^9
  • The queue's maximum capacity can be considered infinite.
Note:
  1. You are not required to display the output as it has already been handled. Just implement the functions.
  2. Use stack data structures only for this implementation.
  3. Leverage inbuilt stack functionalities in languages like C++ and Java if available.
AnswerBot
4mo

Implement a queue using stacks following FIFO principle.

  • Use two stacks to simulate a queue - one for enqueue and one for dequeue operations.

  • For enQueue operation, push elements onto the enqueue stack....read more

Help your peers!
Select
Add answer anonymously...

Virtusa Consulting Services Full Stack Developer interview questions & answers

A Full Stack Developer was asked Q. Minimum Number of Swaps to Sort an Array Find the minimum number of swaps requir...read more
A Full Stack Developer was asked Q. Can you describe the use case of Account Statements Transcription as a full stac...read more
A Full Stack Developer was asked Q. Queue Using Stacks Implementation Design a queue data structure following the FI...read more

Popular interview questions of Full Stack Developer

A Full Stack Developer was asked Q1. Given an array of integers, sort the array in ascending order.
A Full Stack Developer was asked Q2. Minimum Number of Swaps to Sort an Array Find the minimum number of swaps requir...read more
A Full Stack Developer was asked Q3. Can you describe the use case of Account Statements Transcription as a full stac...read more
Virtusa Consulting Services Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits