Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
A tool only gives you stacks: you can push on top and pop from the top. A job needs a queue, where the item that went in first comes out first. You can build one out of two stacks.
Write run_queue(operations). operations is a list of ["enqueue", value], ["dequeue"] and ["peek"] lists. Run them in order. Return a list with one result for each dequeue and peek, in order.
Function to write
run_queue(operations: list[list]) -> listA list with one result for each dequeue and peek, in order; None when the queue is empty.
How to approach it
Use one stack for new items and one for items waiting to leave. When the leaving stack is empty, pour the other stack into it. Pouring reverses the order.
Sample cases
+ 2 held back until you submit
first in, first out
`a` went in first, so it comes out first. Then `b` is at the front, so `peek` shows `b`.
Input
Argument 1
[
[
'enqueue',
'a'
],
[
'enqueue',
'b'
],
[
'enqueue',
'c'
],
[
'dequeue'
],
[
'peek'
],
[
'dequeue'
]
]Returns
[
'a',
'b',
'b'
]mixing adds and removes
New items added after a dequeue must still wait behind the older ones: 1, 2, then 3.
Input
Argument 1
[
[
'enqueue',
1
],
[
'enqueue',
2
],
[
'dequeue'
],
[
'enqueue',
3
],
[
'dequeue'
],
[
'dequeue'
]
]Returns
[
1,
2,
3
]empty queue
Dequeue and peek on an empty queue return None.
Input
Argument 1
[
[
'dequeue'
],
[
'peek'
]
]Returns
[
None,
None
]no operations
Nothing to do, so no results.
Input
Argument 1
[] (empty list)
Returns
[] (empty list)
Constraints
enqueue adds a value at the back and adds nothing to the results.dequeue removes the value at the front and adds it to the results.peek adds the value at the front to the results and removes nothing.dequeue and peek add None.append and pop() from the end of each, as a stack would.Worked example
Take enqueue a, enqueue b, enqueue c, dequeue, peek, dequeue. All three go onto the incoming stack: a, b, c with c on top. dequeue finds outgoing empty, so it moves everything over, one at a time from the top: c, then b, then a. Now outgoing has c, b, a with a on top. It pops a. peek looks at the top of outgoing, which is b. The next dequeue pops b. The results are ['a', 'b', 'b'].
The trap: if you only use one stack, dequeue takes the newest item (c) first. That is a stack, not a queue.
What this tests
Queue using stacks, the idea behind 'Implement Queue using Stacks'. Reversing a stack by moving every item into a second stack puts the oldest item on top. The key rule is to move items only when the outgoing stack is empty, which makes the average cost of each operation O(1).
run_queue(operations: list[list]) -> listSubmit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.