Code Room
CodingMediumcod-g590
Subject QueuesLevel Mid–Senior~20 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Implement a LIFO stack using only standard FIFO queue operations (enqueue at back, dequeue from front, peek front, size). Replay operations ["push", x], ["pop"], ["top"], ["empty"] and return the list of results from pop/top/empty (empty returns 1 for true, 0 for false; pop and top return the value). Make push do the rotation work so pop and top are O(1). Assume pop/top are not called on an empty stack.

Implement
stack_via_queue(ops: list[list]) → list[int]
Examples
in[[["push",1],["push",2],["top"],["pop"],["empty"]]]out[2,2,0]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.