PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates understanding of the command pattern, state and history management, failure handling, and appropriate data structure use for implementing undo semantics, measuring the ability to design clear abstractions that maintain correct system state.

  • hard
  • Netflix
  • Coding & Algorithms
  • Software Engineer

Design command executor with undo

Company: Netflix

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

## Problem Design and implement a command-execution framework that supports **executing commands** and **undoing** the most recently executed command. ### Requirements Implement a class (or set of classes) that supports: - `execute(command)`: applies a command to the system state. - `undo()`: reverts the **most recent successfully executed** command. ### Command interface Assume each command object supports: - `do()`: applies the change. - `undo()`: reverts the change. ### Behavioral details - Commands are executed sequentially. - `undo()` should undo only the last executed command (stack semantics). - If `undo()` is called when there is nothing to undo, handle it gracefully (e.g., no-op or error—state your choice). - Discuss how you would handle failures (e.g., `do()` throws) and whether the command should be recorded in history. ### Example (illustrative) If commands mutate an integer counter: 1. execute(+5) → counter = 5 2. execute(*3) → counter = 15 3. undo() → counter = 5 4. undo() → counter = 0 ### Constraints - Aim for clean abstractions and correct state/history handling. - Time and space should be efficient for long histories.

Quick Answer: This question evaluates understanding of the command pattern, state and history management, failure handling, and appropriate data structure use for implementing undo semantics, measuring the ability to design clear abstractions that maintain correct system state.

You are given a list of commands for an integer counter that starts at 0. Implement the core logic of a command executor that supports executing commands and undoing the most recent successfully executed command. Each command is represented as a tuple: - ('ADD', x): add x to the counter - ('MUL', x): multiply the counter by x - ('DIV', x): divide the counter by x only if x != 0 and the result stays an integer; otherwise the command fails - ('SET', x): set the counter to x - ('UNDO',): undo the most recent successful non-UNDO command Rules: - Commands are processed from left to right. - UNDO follows stack semantics: it reverts only the last successful command that has not already been undone. - If UNDO is called when there is nothing to undo, do nothing. - Failed DIV commands leave the counter unchanged and are not recorded in history. - UNDO itself is never recorded in history. Return the final value of the counter after all commands are processed.

Constraints

  • 0 <= len(operations) <= 200000
  • Each numeric value x is an integer in the range [-10^9, 10^9]
  • Every command is one of ('ADD', x), ('MUL', x), ('DIV', x), ('SET', x), or ('UNDO',)
  • The counter value after every successful command fits in a signed 64-bit integer

Examples

Input: ([],)

Expected Output: 0

Explanation: No commands are executed, so the counter remains at its initial value 0.

Input: ([('ADD', 5), ('MUL', 3), ('UNDO',), ('UNDO',)],)

Expected Output: 0

Explanation: 0 -> 5 -> 15 -> undo to 5 -> undo to 0.

Input: ([('UNDO',), ('ADD', 2), ('UNDO',), ('UNDO',)],)

Expected Output: 0

Explanation: The first and last UNDO do nothing because history is empty. The ADD 2 is undone by the middle UNDO.

Input: ([('ADD', 10), ('DIV', 0), ('UNDO',)],)

Expected Output: 0

Explanation: DIV by 0 fails, so it is not recorded. UNDO removes the earlier successful ADD 10 and restores the counter to 0.

Input: ([('ADD', 7), ('DIV', 2), ('MUL', 4), ('UNDO',)],)

Expected Output: 7

Explanation: DIV 2 fails because 7 is not evenly divisible by 2. Then MUL 4 makes the counter 28, and UNDO restores it to 7.

Input: ([('SET', 4), ('ADD', -6), ('MUL', -2), ('UNDO',), ('UNDO',)],)

Expected Output: 4

Explanation: 0 -> 4 -> -2 -> 4 -> undo to -2 -> undo to 4.

Input: ([('ADD', 3), ('MUL', 0), ('ADD', 5), ('UNDO',), ('UNDO',), ('UNDO',)],)

Expected Output: 0

Explanation: 0 -> 3 -> 0 -> 5 -> undo to 0 -> undo to 3 -> undo to 0. This shows why storing previous states works well for undo.

Hints

  1. Use a stack to remember enough information to undo the last successful command.
  2. For this counter-based version, storing the previous counter value before each successful command makes every undo operation simple, even for commands like MUL 0 or SET.
Last updated: May 1, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Cache, Undo, and DFS - Netflix
  • Implement Streaming Word Counter - Netflix (medium)
  • Implement TTL Cache and Tree Balance Reporting - Netflix (medium)
  • Implement ordering and undo executor - Netflix (medium)
  • Implement Caches, Undo, and Traversal - Netflix