Challenge 4: Implement a Queue Using Stacks

You 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 by using the myStack class 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(): It 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 80+ hands-on prep courses.