A records vault moves sealed cartridges using a single conveyor lane. The lane only supports two physical motions: load a cartridge onto the back of the lane, and unload whichever cartridge currently sits at the front — a strict first-in-first-out queue. Even so, the vault's recall register must behave like a last-in-first-out stack: whichever cartridge was loaded most recently is always the one an archivist gets back first, and internally you may never keep a second stack or index directly into anything but that one FIFO lane.
Process the operations below, in order, against the register:
PUSH x — load a cartridge carrying integer id x.POP — unload the most recently loaded cartridge still in the register and report its id.TOP — report the id of the most recently loaded cartridge still in the register, without unloading it.EMPTY — report whether the register currently holds no cartridges.n, the number of operations.n lines holds one operation: PUSH x, POP, TOP, or EMPTY.Print one line for every POP, TOP, and EMPTY operation, in the order they appear in the input: the reported id for POP/TOP, or true/false for EMPTY. PUSH produces no output.
POP and TOP only ever appear while the register holds at least one cartridge.Example 1
Input
5 PUSH 1 PUSH 2 TOP POP EMPTY
Expected
2 2 false
Explanation
PUSH 1 then PUSH 2 load ids 1 and 2, so 2 is now the most recently loaded. TOP reports 2 without removing it. POP removes and reports 2, leaving only id 1 in the register. EMPTY then reports false because id 1 is still present.
Example 2
Input
6 PUSH 3 PUSH 3 POP POP PUSH 7 TOP
Expected
3 3 7
Explanation
Two cartridges carrying id 3 are loaded back to back. The first POP removes the more recently loaded of the two (reporting 3) and the second POP removes the remaining one (also reporting 3, since both share the same id), leaving the register empty. PUSH 7 loads a fresh cartridge, and TOP reports its id, 7, without removing it.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →