Implement Queue using Stacks
Implement a first-in-first-out queue in the StackQueue class using two stacks. The no-argument constructor creates an empty queue. push(x) adds x at the rear;…
Implement a first-in-first-out queue in the StackQueue class using two stacks. The no-argument constructor creates an empty queue. push(x) adds x at the rear; pop() removes and returns the front; peek() returns the front without removal; isEmpty() reports whether the queue has no elements. Empty pop and peek return -1. Input contains equally sized operations and arguments arrays, with StackQueue first and exactly one argument array per call. Return one result per call: null for construction and push, an integer for pop/peek, and a boolean for isEmpty.
- 1 <= operations.length <= 100, including the initial constructor. - arguments.length equals operations.length; method names and arities must match. - 1 <= x <= 100 for every push; all other calls take no arguments. - There is exactly one constructor, at position zero. - Empty pop/peek are permitted and return -1 without mutation. - Visualization presets contain at most 12 calls; grading retains the full domain.
Example 1: input operations = ["StackQueue","push","push","pop","push","peek","pop","pop"], arguments = [[],[4],[8],[],[5],[],[],[]]; output [null,null,null,4,null,8,8,5]. After removing 4, adding 5 does not overtake 8. Peek leaves 8 in place, so the next pop also returns 8.
Example 2: input operations = ["StackQueue","pop","peek","isEmpty"], arguments = [[],[],[],[]]; output [null,-1,-1,true]. Empty reads return -1 and do not add or remove anything. The final emptiness check is true.
Example 3: input operations = ["StackQueue","push","pop","isEmpty","push","peek","pop","isEmpty"], arguments = [[],[7],[],[],[9],[],[],[]]; output [null,null,7,true,null,9,9,true]. After the only item is removed, the queue is empty. A later push makes 9 the front, and peek does not remove it.
Category: Queues. Topic: queue. Difficulty: Easy. Patterns: stack.