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 the enqueue() and dequeue() functions 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( ): A value to insert into the queue

  • dequeue( ): Does not require any input

Output

  • enqueue( ): Does not return anything

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

Sample Input

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

Sample Output

True # [1, 2, 3, 4, 5]
1 # [2, 3, 4, 5]

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