KatInterview

Append-Only Editor with Undo and Redo Replay

Data Structure DesignAuto-graded · 19 test cases
MediumAuto-gradedData Structure Designdesignstacksimulation

Last updated

Simulate a tail-only text buffer with undo and redo histories, recording only edits that change the text and clearing redo on every new edit.

Replay an Editor Session

You are given a log of commands issued to a very simple text buffer. Edits only ever happen at the end of the text. Replay the commands in order, starting from an empty buffer, and return one result per command.

Commands

commands[i] is the name of the i-th command and params[i] is the list of its arguments.

CommandArgumentsBehavior
insert[text]Append text to the end of the buffer.
delete[k]Remove up to k characters from the end. If k is larger than the current length, remove everything that is there.
undo_last[]Reverse the most recent recorded edit.
redo_last[]Re-apply the edit most recently reversed by an undo.
get_current_content[]Report the current text of the buffer.

History rules

  • Keep two histories: an undo history and a redo history.
  • An insert or delete is recorded only if it actually changes the buffer. Inserting an empty string, deleting when k <= 0, and deleting from an empty buffer change nothing. They are not recorded and they do not clear the redo history.
  • A recorded edit is pushed onto the undo history, and the redo history is emptied.
  • A delete must remember the characters it really removed, not the requested k. For example, deleting 100 characters from a 3-character buffer records exactly those 3 characters.
  • undo_last takes the newest edit from the undo history, reverses its effect on the buffer, and moves it to the redo history. If the undo history is empty, nothing happens.
  • redo_last takes the newest edit from the redo history, applies it again, and moves it back to the undo history. If the redo history is empty, nothing happens. Calling it repeatedly after the redo history runs out has no further effect.
  • If a command name is not one of the five above, raise ValueError.

Output

Return a list with one entry per command, in order:

  • insert, delete, undo_last, and redo_last produce None, whether or not they changed anything.
  • get_current_content produces the current text as a string.

Constraints

  • len(commands) == len(params).
  • Every insert has exactly one string argument. Every delete has exactly one integer argument, which may be zero, negative, or far larger than the buffer length.
  • Undo and redo may be issued when their histories are empty.
  • The total number of inserted characters fits comfortably in memory.
  • Aim for work proportional to the characters actually touched by each command, not to the requested k or to the whole history.

Examples

Example 1

Input: simulate_editor_session(['insert', 'get_current_content', 'undo_last', 'get_current_content', 'redo_last', 'get_current_content'], [['hello'], [], [], [], [], []])

Output: [None, 'hello', None, '', None, 'hello']

Example 2

Input: simulate_editor_session(['insert', 'insert', 'delete', 'get_current_content', 'undo_last', 'get_current_content', 'undo_last', 'get_current_content'], [['abcdef'], ['XYZ'], [4], [], [], [], [], []])

Output: [None, None, None, 'abcde', None, 'abcdefXYZ', None, 'abcdef']

Example 3

Input: simulate_editor_session(['delete', 'undo_last', 'redo_last', 'get_current_content', 'redo_last', 'get_current_content'], [[100], [], [], [], [], []])

Output: [None, None, None, '', None, '']

Hints

Hint 1

All changes happen at the end of the text, so the buffer behaves like a stack of characters. A Python list of characters can be extended or truncated at the tail cheaply.

Hint 2

Store each recorded edit as a pair (kind, text). For an insert the text is what was added. For a delete it is the characters that were really removed. That alone is enough to apply the edit or build its inverse.

Hint 3

Skip edits that change nothing, and clamp delete to min(max(k, 0), len(buffer)). Beware that del buf[-0:] wipes the whole list, so never run a truncation with length zero. Clear the redo history only when an edit is recorded.