MicrosoftOA #31
Implement Queue using Stacks (Round 2, LC 232
Estimated EasyJudge-ready
Topics (AI-labelled)IteratorsStacks & Heaps
The queue starts empty. Implement push, pop, peek, and empty using only two stacks. Output the return value of every operation, with null for push; this site does not output anything for the constructor. The judge checks behavior only, so please respect the two-stack restriction in your implementation.
Input
The first line contains the number of operations Q (1..100), followed by Q lines: push x (1..9), pop, peek, or empty. The queue is guaranteed to be non-empty for pop and peek.
Output
Output the Q return values on one line, separated by spaces, using null, true, false, or integers.
Example 1
Input:
5
push 1
push 2
peek
pop
empty
Output:
null null 1 1 false
Example 2
Input:
1
empty
Output:
true
Example 3
Input:
3
push 9
pop
empty
Output:
null 9 true
Example notes
Example 1: the return values are null, null, 1, 1, false in order, and 2 remains in the queue at the end.
Example 2: the queue starts empty, so it returns true.
Example 3: 9 is enqueued and then removed, leaving the queue empty; the return values are null, 9, true.
Editorial
Loading solution…