Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-005

Queue Implemented with Two Stacks

Problem

Implement a first-in-first-out queue using two stacks.

operations contains "push", "pop", "peek", or "empty". values provides the element for push (ignored otherwise). Record None for push, the removed front element (or None if empty) for pop, the front element (or None if empty) for peek, and a boolean for empty.

Return the list of recorded results.

Hint

Push onto an inbox stack; when a pop is needed, move everything to an outbox stack.

Test cases

case 1
in: [["push","push","peek","pop","empty"],[1,2,0,0,0]]
expected: [null,null,1,1,false]
case 2
in: [["pop","push","pop","pop"],[0,5,0,0]]
expected: [null,null,5,null]
case 3
in: [["push","pop","pop","empty"],[7,0,0,0]]
expected: [null,7,null,true]
case 4
in: [["empty"],[0]]
expected: [true]
Starter codePython
def two_stack_queue(operations, values):
    # Your code here
    pass

4 test cases · run your code against them in the editor