Stack

Python data engineering interview problem. Difficulty: intermediate. Pattern: Stacks. About 14 minutes. Part of the Pro drill bank.

Implement a basic Stack with push, pop, top, and empty. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

A stack models work where the newest item must be handled first, such as undo actions. This is called last in, first out. Implement a Stack class with push, pop, top and empty. pop removes and returns the newest value, while top reads it without removing it. Write class Stack. After pushing 1 and 2, pop() returns 2 and the next top() returns 1. The starter file already declares the class shape and harness prints. Keep the class name, method names and print lines, and fill in the methods.

Requirements

  • Push values onto the top.
  • Pop and return the most recently pushed value.
  • Return a boolean from empty and leave top non-mutating.

Constraints

  • The stack begins empty and stores arbitrary simple values.
  • The starter harness calls the class methods, so method names and return values must match.

Examples

Input: Stack push 1,2; pop; top Output: 2 then 1 LIFO order.

Topics: lakebench, python, stack, class.

More Python interview questions · All interview problems · Learn data engineering

intermediate

Stack

Interview-style drill: Implement a basic Stack with push, pop, top, and empty.

Implement class `Stack` with methods `push(x)`, `pop()` (return value), `top()`, and `empty()` → bool. Keep the harness prints.