Build an undo/redo system for ADD, SUB and MUL commands using the Command pattern

Quick Overview

Build an undo and redo system with the Command pattern, where ADD, SUB and MUL commands change an integer state. It tests how each command is recorded so it can be reversed exactly, how the undo and redo histories interact, and why a new command clears the redo history.

Build an undo/redo system for ADD, SUB and MUL commands using the Command pattern

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a system that supports undoing and redoing actions in a tool or editor using the Command pattern. The system manages a state value (initially `0`) and supports executing commands that modify the state, undoing the most recent command, and redoing the most recently undone command. Supported command types are `"ADD"` (add a value to state), `"SUB"` (subtract a value from state), and `"MUL"` (multiply state by a value). When a new command is executed after undoing, the redo history is cleared. Implement the following methods: - `execute(command_type: str, value: int) -> int`: Execute a command of the given type (`"ADD"`, `"SUB"` or `"MUL"`) with the given value. Clears the redo stack. Returns the new state. - `undo() -> bool`: Undo the most recent command. Returns True if successful, False if there is nothing to undo. - `redo() -> bool`: Redo the most recently undone command. Returns True if successful, False if there is nothing to redo. - `get_state() -> int`: Return the current state value. Example 1: ```text Input: ["UndoRedoSystem", "execute", "execute", "execute", "get_state", "undo", "get_state", "undo", "get_state", "redo", "get_state"] [[], ["ADD", 5], ["SUB", 3], ["MUL", 4], [], [], [], [], [], [], []] Output: [null, 5, 2, 8, 8, true, 2, true, 5, true, 2] Explanation: editor = UndoRedoSystem() # state = 0 editor.execute("ADD", 5) # → 5 (0 + 5 = 5) editor.execute("SUB", 3) # → 2 (5 - 3 = 2) editor.execute("MUL", 4) # → 8 (2 * 4 = 8) editor.get_state() # → 8 editor.undo() # → true (undoes MUL 4, state = 2) editor.get_state() # → 2 editor.undo() # → true (undoes SUB 3, state = 5) editor.get_state() # → 5 editor.redo() # → true (redoes SUB 3, state = 2) editor.get_state() # → 2 ``` Example 2: ```text Input: ["UndoRedoSystem", "undo", "redo", "execute", "undo", "redo", "execute", "redo", "get_state"] [[], [], [], ["ADD", 10], [], [], ["ADD", 20], [], []] Output: [null, false, false, 10, true, true, 30, false, 30] Explanation: editor = UndoRedoSystem() # state = 0 editor.undo() # → false (nothing to undo) editor.redo() # → false (nothing to redo) editor.execute("ADD", 10) # → 10 (0 + 10 = 10) editor.undo() # → true (undoes ADD 10, state = 0) editor.redo() # → true (redoes ADD 10, state = 10) editor.execute("ADD", 20) # → 30 (10 + 20 = 30, clears redo stack) editor.redo() # → false (redo stack was cleared) editor.get_state() # → 30 ``` ```hint Try to invert MUL Before choosing how undo works, check whether every command type can be reversed from its result alone. Try MUL with value 0. ``` ```hint One class per command Give each command type a small class with a shared interface, so the system class needs no branch per command type in execute, undo or redo. ``` ### Constraints & Assumptions - Initial state is `0`. - Command types: `"ADD"`, `"SUB"`, `"MUL"`. - `-10⁴ <= value <= 10⁴` - At most `10⁴` calls total across all methods. - `undo()` on an empty undo stack returns `false`. - `redo()` on an empty redo stack returns `false`. - Executing a new command clears the redo stack. ### Clarifying Questions to Ask - What should `execute` do with an unknown command type: raise an error, or ignore it without recording anything? - Repeated MUL makes the state grow fast. Should it wrap at 64 bits, raise an error on overflow, or use arbitrary precision? - Will more command types be added later (for example DIV or SET), and can any of them lack an exact inverse? ### What a Strong Answer Covers - The command abstraction: what each command object holds and which operations it exposes. - How undo reverses every command type exactly, including the cases where an arithmetic inverse fails. - How commands move between the undo and redo histories, and when the redo history is cleared. - The time and space cost per operation, and what happens as the state value grows. - How a new command type would be added without editing the system class. ### Follow-up Questions - How would you group several commands into one undoable step, such as a macro or a transaction? - If the state were a large document instead of one integer, what would each command store for undo, and what would that cost? - How would you add a DIV command with integer division, and can it be undone without saving the previous state? - How would you cap the history so memory stays bounded?

Overview: Build an undo and redo system with the Command pattern, where ADD, SUB and MUL commands change an integer state. It tests how each command is recorded so it can be reversed exactly, how the undo and redo histories interact, and why a new command clears the redo history.

Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a system that supports undoing and redoing actions in a tool or editor using the Command pattern.

The system manages a state value (initially 0) and supports executing commands that modify the state, undoing the most recent command, and redoing the most recently undone command.

Supported command types are "ADD" (add a value to state), "SUB" (subtract a value from state), and "MUL" (multiply state by a value). When a new command is executed after undoing, the redo history is cleared.

Implement the following methods:

  • execute(command_type: str, value: int) -> int : Execute a command of the given type ( "ADD" , "SUB" or "MUL" ) with the given value. Clears the redo stack. Returns the new state.
  • undo() -> bool : Undo the most recent command. Returns True if successful, False if there is nothing to undo.
  • redo() -> bool : Redo the most recently undone command. Returns True if successful, False if there is nothing to redo.
  • get_state() -> int : Return the current state value.

Example 1:

Input:
["UndoRedoSystem", "execute",  "execute",  "execute",  "get_state", "undo", "get_state", "undo", "get_state", "redo", "get_state"]
[[],               ["ADD", 5], ["SUB", 3], ["MUL", 4], [],          [],     [],          [],     [],          [],     []]

Output: [null, 5, 2, 8, 8, true, 2, true, 5, true, 2]

Explanation:
editor = UndoRedoSystem()            # state = 0

editor.execute("ADD", 5)             # → 5     (0 + 5 = 5)
editor.execute("SUB", 3)             # → 2     (5 - 3 = 2)
editor.execute("MUL", 4)             # → 8     (2 * 4 = 8)
editor.get_state()                   # → 8
editor.undo()                        # → true  (undoes MUL 4, state = 2)
editor.get_state()                   # → 2
editor.undo()                        # → true  (undoes SUB 3, state = 5)
editor.get_state()                   # → 5
editor.redo()                        # → true  (redoes SUB 3, state = 2)
editor.get_state()                   # → 2

Example 2:

Input:
["UndoRedoSystem", "undo", "redo", "execute",   "undo", "redo", "execute",   "redo", "get_state"]
[[],               [],     [],     ["ADD", 10], [],     [],     ["ADD", 20], [],     []]

Output: [null, false, false, 10, true, true, 30, false, 30]

Explanation:
editor = UndoRedoSystem()            # state = 0

editor.undo()                        # → false (nothing to undo)
editor.redo()                        # → false (nothing to redo)
editor.execute("ADD", 10)            # → 10    (0 + 10 = 10)
editor.undo()                        # → true  (undoes ADD 10, state = 0)
editor.redo()                        # → true  (redoes ADD 10, state = 10)
editor.execute("ADD", 20)            # → 30    (10 + 20 = 30, clears redo stack)
editor.redo()                        # → false (redo stack was cleared)
editor.get_state()                   # → 30

Constraints & Assumptions

  • Initial state is 0 .
  • Command types: "ADD" , "SUB" , "MUL" .
  • -10⁴ <= value <= 10⁴
  • At most 10⁴ calls total across all methods.
  • undo() on an empty undo stack returns false .
  • redo() on an empty redo stack returns false .
  • Executing a new command clears the redo stack.

Clarifying Questions to Ask Guidance

  • What should execute do with an unknown command type: raise an error, or ignore it without recording anything?
  • Repeated MUL makes the state grow fast. Should it wrap at 64 bits, raise an error on overflow, or use arbitrary precision?
  • Will more command types be added later (for example DIV or SET), and can any of them lack an exact inverse?

What a Strong Answer Covers Guidance

  • The command abstraction: what each command object holds and which operations it exposes.
  • How undo reverses every command type exactly, including the cases where an arithmetic inverse fails.
  • How commands move between the undo and redo histories, and when the redo history is cleared.
  • The time and space cost per operation, and what happens as the state value grows.
  • How a new command type would be added without editing the system class.

Follow-up Questions Guidance

  • How would you group several commands into one undoable step, such as a macro or a transaction?
  • If the state were a large document instead of one integer, what would each command store for undo, and what would that cost?
  • How would you add a DIV command with integer division, and can it be undone without saving the previous state?
  • How would you cap the history so memory stays bounded?