Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Implement Stack using Queues Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Implement Stack using Queues

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Implement a stack using one or more queues.

The Stack class should support the following methods:

  • int size()
  • boolean isEmpty()
  • int top()
  • void push(int element)
  • void pop()

You can assume that you've access to a Queue class that provides the following methods:

  • int size()
  • boolean isEmpty()
  • int front()
  • int back()
  • void push(int element)
  • void pop()

Approach

All the operations of the stack can be implemented with a single queue. All the functions except the push() function can be implemented using the corresponding functions of the Queue class. For the push() function, whenever we push an element in the queue, take all the elements in the queue, except the current one, and push them into the queue again. Refer to the implementation for more details.

Analysis

  • Time Complexity: O(n) for push(), O(1) for the rest.
  • Space Complexity: O(n) for push(), O(1) for the rest.

Implementation

C++
/* Use this Queue class
class Queue {
	Queue (int capacity)
	int size()
	boolean isEmpty()
	int front()
	int back()
	void push(int element)
	void pop()
};
*/

// Implement the Stack class
class Stack {
public:
	int capacity;
	Queue *queue;
	Stack (int capacity) {
		this->capacity = capacity;
		queue = new Queue(capacity);
	}
	bool isEmpty() {
		return queue->isEmpty();
	}
	
	int size() {
		return queue->size();
	}
	
	int top() {
		return queue->front();
	}
	
	void push(int element) {
		queue->push(element);
		for(int i = 1; i < queue->size(); i++) {
			queue->push(queue->front());
			queue->pop();
		}
	}
	
	void pop() {
		queue->pop();
	}
};
Java
/* Use this Queue class
class Queue {
	Queue (int capacity)
	int size()
	boolean isEmpty()
	int front()
	int back()
	void push(int element)
	void pop()
};
*/

// Implement the Stack class
class Stack {
	Queue queue;
	int capacity;
	public Stack (int capacity) {
		this.capacity = capacity;
		queue = new Queue(capacity);
	}

	public boolean isEmpty() {
		return queue.isEmpty();
	}
	
	public int size() {
		return queue.size();
	}
	
	public int top() {
		return queue.front();
	}
	
	public void push(int element) {
		queue.push(element);
		for(int i = 1; i < queue.size(); i++) {
			queue.push(queue.front());
			queue.pop();
		}
	}
	
	public void pop() {
		queue.pop();
	}
}
Related Content
Balanced Parentheses
Evaluate Reverse Polish Notation
Implement Queue using Array
Implement Queue using Linked List
Implement Queue using Stacks
Implement Queue using Stacks
Implement Stack using Array
Implement Stack using Linked List
Largest Rectangle in Histogram
Implement Min Stack
Next Greater Element
Rotting Apples
Sliding Window Maximum
Trapping Rain Water
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet