Implement Queue using Stacks
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Build a first-in-first-out queue, MyQueue, whose only internal storage is **two stacks**. Treat each stack as supporting only push-to-top, pop-from-top, peek-top, size and is-empty (a Python list used with append/pop()/[-1]/len is fine).
Implement:
- MyQueue() creates an empty queue.
- push(x) appends x to the back of the queue.
- pop() removes and returns the element at the front.
- peek() returns the front element without removing it.
- empty() returns true if the queue holds no elements, otherwise false.
pop and peek are only called when the queue is non-empty. Aim for **amortized O(1)** per operation: across any sequence of n calls the total work should be O(n).
Examples
Constraints
- 1 <= x <= 100
- At most 1000 calls are made in total.
- `pop` and `peek` are never called on an empty queue.
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.