The Queue Disguise
A medium Python interview practice problem on DataDriven. Write and execute real python code with instant grading.
- Domain
- Python
- Difficulty
- medium
- Seniority
- L4
Problem
Implement a FIFO queue class `MyQueueFromStacks` using two stacks (Python lists), supporting `push(x)`, `pop()` (remove and return the front value), `peek()` (return the front value without removing), and `empty()` (return a bool). Then write a top-level driver `run_queue_operations(operations, args)` that the test harness calls. `operations` is a list of strings: the first entry is always `"MyQueueFromStacks"` (construct a fresh queue), and each later entry is one of `"push"`, `"pop"`, `"peek"`, or `"empty"`. `args` is a parallel list of argument lists (e.g. `[1]` for a push, `[]` for the others). Replay the operations in order and return a list parallel to `operations`: `None` for the constructor and for each `push`, the returned value for each `pop`/`peek`, and a `True`/`False` bool for each `empty`. Inputs are well-formed: `pop`/`peek` are never called on an empty queue.
Summary
A queue in sheep's clothing.
Practice This Problem
Solve this Python problem with real code execution. DataDriven runs your Python code in a real environment and grades it automatically.