Gamified challenge · Stacks
A box office must serve fans first come, first served, but its only storage is two stacks (you may only push onto or pop from the top of each). You get the day's operations in order: "join X" puts a fan with ticket number X at the back of the line, "serve" serves the fan at the front and hands out their ticket number, and "peek" reports the front fan's ticket number without serving them.
Write runOps(ops) (Python: run_ops(ops)) that returns a list with one number for every `serve` and `peek`, in order: the ticket number, or -1 if the line is empty at that moment. join adds nothing to the output. Solve it with two stacks (an inbox and an outbox); a built-in queue gives the same output but misses the point.
For ["join 5", "join 7", "serve", "join 9", "peek", "serve", "serve"] the answer is [5, 7, 7, 9].
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google