All problems
Stack Using a Queue
Problem
Simulate a stack using only a single queue.
operations contains "push", "pop", "top", or "empty"; values provides the pushed value (ignored otherwise). On push, append the value and then rotate all earlier elements behind it so the newest element is always at the front. Record None for push, the removed value (or None if empty) for pop, the top value (or None if empty) for top, and a boolean for empty.
Return the list of recorded results.
Hint
After appending, rotate the new element to the front by moving n-1 elements to the back.
Test cases
case 1
in: [["push","push","top","pop","empty"],[1,2,0,0,0]]
expected: [null,null,2,2,false]
case 2
in: [["pop","top","empty"],[0,0,0]]
expected: [null,null,true]
case 3
in: [["push","pop","pop"],[5,0,0]]
expected: [null,5,null]
case 4
in: [["push","push","push","pop"],[1,2,3,0]]
expected: [null,null,null,3]
Starter codePython
def queue_stack_ops(operations, values):
# Your code here
pass4 test cases · run your code against them in the editor