Sign in to track progress, unlock prep layers, and browse your pipeline.Sign in
Implement text editor with undo/redo
Implement Text Editor with Undo/Redo
Implement a TextDocument class that supports basic text manipulation (inserting and deleting characters from the end) along with full undo/redo functionality.
Provided Skeleton
class Operation:
pass
class InsertAtEndOperation(Operation):
def __init__(self, chars_to_insert: str):
self.chars_to_insert = chars_to_insert
class DeleteFromEndOperation(Operation):
def __init__(self, num_chars_to_delete: int):
self.num_chars_to_delete = num_chars_to_delete
class TextDocument:
def __init__(self) -> None:
pass
def apply_operation(self, op: Operation) -> None:
pass
def get_current_content(self) -> str:
return self.doc # Assuming 'doc' is the internal representation
def undo_last(self) -> None:
pass
def redo_last(self) -> None:
pass
Requirements
apply_operation(self, op: Operation):- Must support appending
chars_to_insertto the end of the document. - Must support deleting up to
num_chars_to_deletecharacters from the end of the document. If fewer characters exist than requested, delete all available characters without error.
- Must support appending
get_current_content(): Returns the current string content of the document. The document starts empty.undo_last(): Reverts the most recently applied operation.redo_last(): Reapplies the most recently undone operation.- History Management: After any successful
apply_operation, the entire redo history must be cleared. - Performance: Ensure each operation (
apply_operation,undo_last,redo_last) runs in time proportional to the size of the change (e.g., length of inserted string, number of deleted characters), specifically avoiding copying the entire document per step. - Discussion: Be prepared to discuss:
- The chosen internal data structures (e.g., stacks, chunked buffers).
- How inverse operations are computed and stored.
- Time and space complexity of each method.
- Key edge cases (e.g., deleting more characters than available, performing multiple undos/redos, applying new operations after undos).
AI interview prep
Core dilemma
Balancing efficient, mutable string manipulation (specifically at the end) with robust, performant, and memory-efficient undo/redo history management, all while avoiding full document copies for each operation.
Mid-level signals3
Senior signals3
Red flags3
2 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement text editor with undo/redo
Implement Text Editor with Undo/Redo
Implement a TextDocument class that supports basic text manipulation (inserting and deleting characters from the end) along with full undo/redo functionality.
Provided Skeleton
class Operation:
pass
class InsertAtEndOperation(Operation):
def __init__(self, chars_to_insert: str):
self.chars_to_insert = chars_to_insert
class DeleteFromEndOperation(Operation):
def __init__(self, num_chars_to_delete: int):
self.num_chars_to_delete = num_chars_to_delete
class TextDocument:
def __init__(self) -> None:
pass
def apply_operation(self, op: Operation) -> None:
pass
def get_current_content(self) -> str:
return self.doc # Assuming 'doc' is the internal representation
def undo_last(self) -> None:
pass
def redo_last(self) -> None:
pass
Requirements
apply_operation(self, op: Operation):- Must support appending
chars_to_insertto the end of the document. - Must support deleting up to
num_chars_to_deletecharacters from the end of the document. If fewer characters exist than requested, delete all available characters without error.
- Must support appending
get_current_content(): Returns the current string content of the document. The document starts empty.undo_last(): Reverts the most recently applied operation.redo_last(): Reapplies the most recently undone operation.- History Management: After any successful
apply_operation, the entire redo history must be cleared. - Performance: Ensure each operation (
apply_operation,undo_last,redo_last) runs in time proportional to the size of the change (e.g., length of inserted string, number of deleted characters), specifically avoiding copying the entire document per step. - Discussion: Be prepared to discuss:
- The chosen internal data structures (e.g., stacks, chunked buffers).
- How inverse operations are computed and stored.
- Time and space complexity of each method.
- Key edge cases (e.g., deleting more characters than available, performing multiple undos/redos, applying new operations after undos).