Baseline Stack and Queues · Learning O(1) per operation · O(n)
A fulfillment center loads pallets into a single loading bay where only the most recently placed pallet is reachable. Workers can drop a new pallet onto the bay or haul the top pallet away; anything buried underneath is untouchable until everything above it leaves. You are asked to run this bay: accept drop-off commands and retrieval commands, and report what each retrieval yields.
Input: A list of operations, each either ["push", value] to load a pallet or ["pop"] to remove the top pallet.
Output: Return the list of values produced by every pop operation, in order; a pop on an empty bay yields -1.
1 <= number of operations <= 10^5-10^9 <= pushed values <= 10^9The structure behaves as LIFO: the last pushed element is the first poppedInput: {"operations":[["push",2],["push",3],["pop"],["push",4],["pop"]]}
Output: [3,4]
Push 2 then 3; pop returns the last pushed value 3. Push 4; pop returns 4.
Input: {"operations":[["push",7],["pop"],["pop"]]}
Output: [7,-1]
The first pop yields 7; the second finds an empty stack and returns -1.