Baseline Stack and Queues · Learning O(1) per operation · O(n)
A factory line moves crates onto a conveyor at one end and unloads them from the other, so the crate that arrived first is always the crate that leaves first. The belt controller accepts two commands: drop a crate onto the loading end, or release whatever is waiting at the unloading end. Your job is to replay a command log and report what each release yields.
Input: A list of operations, each either ["push", value] to enqueue or ["pop"] to dequeue.
Output: Return the list of values produced by every pop operation, in order; a pop on an empty queue yields -1.
1 <= number of operations <= 10^5-10^9 <= pushed values <= 10^9The structure behaves as FIFO: the earliest pushed element is the first poppedInput: {"operations":[["push",2],["push",3],["pop"],["push",4],["pop"]]}
Output: [2,3]
First-in-first-out: the 2 leaves before the 3, then the 4 follows.
Input: {"operations":[["push",7],["pop"],["pop"]]}
Output: [7,-1]
The single crate leaves first; the next release finds the belt empty.