As reported: Adobe Computer Scientist 1 interview (Noida, 2025), round 1: Implement Queue using Stacks — a working solution first; the interviewer then asked for the optimized version.
This is LeetCode 232 — Implement Queue using Stacks.
Implement a first-in-first-out queue MyQueue using only two stacks (only push to top, pop/peek from top, size and is-empty):
push(x)addsxto the back of the queue.pop()removes and returns the front element.peek()returns the front element.empty()returns whether the queue is empty.
Every operation should run in amortized O(1) time.
Example
ops = ["MyQueue", "push", "push", "peek", "pop", "empty"]
args = [[], [1], [2], [], [], []]
Output: [null, null, null, 1, 1, false]
Constraints: 1 ≤ x ≤ 9; at most 100 calls; pop and peek are only called on a non-empty queue.
Code runner format: two JSON lines: the operation names and their arguments, as on LeetCode. The harness prints the list of return values (null for MyQueue and push).