Design a notepad with append, delete, undo and redo

Quick Overview

Design a notepad class that appends text, deletes trailing characters, and supports undo and redo, where any new edit clears the redo history. It tests the choice of history data structures, what to record for each operation, edge cases such as deletes longer than the text, and the memory cost of each design.

Design a notepad with append, delete, undo and redo

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a notepad that holds one string of text and supports the following: - Append text to the end of the current content. - Delete the last `n` characters from the content. - Retrieve the current text. - Undo the last `append` or `delete` operation. - Redo the last undone operation. - A new `append` or `delete` operation clears the redo history. Implement the following methods: - `Notepad()` initializes an empty notepad. - `append(text: string) -> void` appends the given text to the end of the current content. - `delete(n: number) -> string` deletes and returns the last `n` characters of the current content. If `n` is greater than the current content length, it deletes and returns all of the content. - `getText() -> string` returns the current content of the notepad. - `undo() -> boolean` undoes the last `append` or `delete` operation. If there is nothing to undo, it returns `false`. Otherwise it reverses the last operation and returns `true`. - `redo() -> boolean` redoes the last undone operation. If there is nothing to redo, it returns `false`. Otherwise it reapplies the operation and returns `true`. Example: ```text Call Returns Text afterwards Notepad() "" append("hello") "hello" append(" world") "hello world" delete(6) " world" "hello" getText() "hello" "hello" undo() true "hello world" redo() true "hello" redo() false "hello" undo() true "hello world" undo() true "hello" append("!") "hello!" redo() false "hello!" delete(10) "hello!" "" ``` The second `redo()` returns `false` because the only undone operation has already been reapplied. The `redo()` after `append("!")` returns `false` because the new append cleared the two operations that had just been undone. The final `delete(10)` asks for more characters than the notepad holds, so it removes and returns all six. ```hint Two histories Undo and redo each act on the most recent entry of their own history. Pick a structure that makes that cheap, and work out what moves between the two histories on each call. ``` ```hint Check the memory Saving a full copy of the text before every edit is the easiest design. Work out its worst-case memory from the constraints before you settle on it. ``` ### Constraints & Assumptions - `1 <= text.length <= 100` - `1 <= n <= 10^5` - At most `10^4` calls in total are made to `append`, `delete`, `getText`, `undo` and `redo`. ### Clarifying Questions to Ask - If `delete` runs on an empty notepad, does it count as an operation that `undo` can reverse, and does it clear the redo history? - Is the undo history unbounded, or should it keep only the most recent operations? - Can the text contain any Unicode, and if so, does a character mean a code point? ### What a Strong Answer Covers - The structures that hold the undo and redo histories, and how each method moves entries between them. - What gets stored for each operation, and what that choice costs in memory under the stated limits. - When the redo history is cleared and when it must be left alone. - Edge cases: `delete` with `n` past the content length, `delete` on an empty notepad, and `undo` or `redo` with nothing to act on. - The time cost of each method. ### Follow-up Questions - How would you cap the history at the last 100 operations without slowing down `undo`? - How would the design change if edits could happen at any cursor position, not only at the end? - How would you group a burst of quick appends into a single undo step? - If the text grew to millions of characters and `getText` ran after every edit, what representation would you pick for the content?

Overview: Design a notepad class that appends text, deletes trailing characters, and supports undo and redo, where any new edit clears the redo history. It tests the choice of history data structures, what to record for each operation, edge cases such as deletes longer than the text, and the memory cost of each design.

PracHub logo
General
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a notepad that holds one string of text and supports the following:

  • Append text to the end of the current content.
  • Delete the last n characters from the content.
  • Retrieve the current text.
  • Undo the last append or delete operation.
  • Redo the last undone operation.
  • A new append or delete operation clears the redo history.

Implement the following methods:

  • Notepad() initializes an empty notepad.
  • append(text: string) -> void appends the given text to the end of the current content.
  • delete(n: number) -> string deletes and returns the last n characters of the current content. If n is greater than the current content length, it deletes and returns all of the content.
  • getText() -> string returns the current content of the notepad.
  • undo() -> boolean undoes the last append or delete operation. If there is nothing to undo, it returns false . Otherwise it reverses the last operation and returns true .
  • redo() -> boolean redoes the last undone operation. If there is nothing to redo, it returns false . Otherwise it reapplies the operation and returns true .

Example:

Call              Returns     Text afterwards
Notepad()                     ""
append("hello")               "hello"
append(" world")              "hello world"
delete(6)         " world"    "hello"
getText()         "hello"     "hello"
undo()            true        "hello world"
redo()            true        "hello"
redo()            false       "hello"
undo()            true        "hello world"
undo()            true        "hello"
append("!")                   "hello!"
redo()            false       "hello!"
delete(10)        "hello!"    ""

The second redo() returns false because the only undone operation has already been reapplied. The redo() after append("!") returns false because the new append cleared the two operations that had just been undone. The final delete(10) asks for more characters than the notepad holds, so it removes and returns all six.

Constraints & Assumptions

  • 1 <= text.length <= 100
  • 1 <= n <= 10^5
  • At most 10^4 calls in total are made to append , delete , getText , undo and redo .

Clarifying Questions to Ask Guidance

  • If delete runs on an empty notepad, does it count as an operation that undo can reverse, and does it clear the redo history?
  • Is the undo history unbounded, or should it keep only the most recent operations?
  • Can the text contain any Unicode, and if so, does a character mean a code point?

What a Strong Answer Covers Guidance

  • The structures that hold the undo and redo histories, and how each method moves entries between them.
  • What gets stored for each operation, and what that choice costs in memory under the stated limits.
  • When the redo history is cleared and when it must be left alone.
  • Edge cases: delete with n past the content length, delete on an empty notepad, and undo or redo with nothing to act on.
  • The time cost of each method.

Follow-up Questions Guidance

  • How would you cap the history at the last 100 operations without slowing down undo ?
  • How would the design change if edits could happen at any cursor position, not only at the end?
  • How would you group a burst of quick appends into a single undo step?
  • If the text grew to millions of characters and getText ran after every edit, what representation would you pick for the content?