AlgoMaster Logo
AlgoMasterDesign Text Editoreasy

Design Text Editor

easy

A text editor turns every edit into a command object. The session keeps the executed commands so any of them can be undone, and undone commands so they can be redone.

TextBuffer and EditorSession are provided in the starter code. Implement only the command types they use: EditCommand, InsertCommand, DeleteLastCommand and ClearCommand (IEditCommand in C#).

The provided TextBuffer is the receiver. getText() returns the text, append(piece) adds to the end, truncate(count) removes up to count characters from the end and returns what it removed, and setText(value) replaces everything.

The provided EditorSession behaves as follows:

  • EditorSession() starts with an empty buffer, nothing to undo and nothing to redo.
  • boolean insert(String text) appends the text and returns true. Empty text changes nothing and returns false.
  • boolean deleteLast(int count) removes up to count characters from the end and returns true. A count below 1, or an empty buffer, changes nothing and returns false.
  • boolean clear() empties the buffer and returns true, or returns false when it is already empty.
  • boolean undo() reverses the most recent command and returns true, or returns false when there is nothing to reverse.
  • boolean redo() runs the most recently undone command again and returns true, or returns false when there is nothing to redo.
  • String text() returns the current text.
  • int length() returns its length.
  • int historySize() returns how many commands are available to undo.

A successful insert, delete or clear clears the redo stack. Redo calls execute on the same command object a second time, so a command must recompute anything it captures.

The tests call the provided EditorSession; your work should be confined to the command contract and the three concrete command classes.

Example 1:

Input:

Output:

Explanation: Two inserts build the text and a delete takes three characters off. The first undo puts those three characters back, the second undo removes the second insert.

Example 2:

Input:

Output:

Explanation: Clear empties the buffer. Undo restores the saved text, redo runs the same clear command again, and both commands stay in history.

Constraints

  • 0 <= text.length <= 40
  • -5 <= count <= 50
  • At most 100 calls in total are made across all methods.

Starter Code

Implement the EditCommand contract and the three concrete commands. TextBuffer and EditorSession are complete and must not be modified.

How the design is graded

needs 7/10 to pass
  • One command contract

    Full marks when `EditCommand` defines `execute` and `undo` and all three concrete commands implement it with the constructors the provided `EditorSession` uses. Lose points when a command needs changes to the session or the buffer.

  • Commands capture what undo needs

    Full marks when `DeleteLastCommand` records the text that actually came off during `execute` and `ClearCommand` records the whole buffer, so undo restores exactly what was there. Lose points heavily when undo guesses, for example by appending a fixed number of characters or restoring nothing.

  • Redo re-runs the same object

    Full marks when a command executed a second time through redo recomputes its captured state rather than reusing stale state, and when insert's undo removes exactly the inserted length. Lose points for printing to stdout.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new EditorSession()null
insert("Hello")true
insert(" World")true
text()"Hello World"
deleteLast(3)true
text()"Hello Wo"
undo()true
text()"Hello World"
undo()true
text()"Hello"

Two inserts build the text and a delete takes three characters off. The first undo puts those three characters back, the second undo removes the second insert.

Run checks these cases. Submit also runs a larger hidden set.