Challenge: Implement a Queue Using Stacks

We have seen the difference between stacks and queues, but is it possible to make one from the other? Let's find out.

Problem Statement #

You have to implement enqueue() and dequeue() functions in the newQueue class using the myStack class we created earlier. enqueue() will insert a value into the queue and dequeue will remove a value from the queue.

Input #

  • enqueue(int value): A value to insert into the queue.

  • dequeue(): Does not require inputs.

Output #

  • enqueue(): Returns True after inserting the value into the queue.

  • dequeue(): Pops out and returns the oldest value in the queue.

Sample Input #

value = 5 // queue = [1, 2, 3, 4] 
enqueue(value)
dequeue()

Sample Output #

True //queue = [1, 2, 3, 4, 5]
1 // queue = [2, 3, 4, 5]

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.