Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Text Editor Data Structure Exercise
00:00
5 left

Text Editor Data Structure Exercise

HardPython

Problem

I'll give you a coding exercise. You have to build a text editor with features like insertion where it inserts a text. A feature like the deletion of n characters before the cursor position. another feature that is undo to revert the last operation and another feature which is redo to reapply the last undone operation. The cursor is always at the end of the text after an insertion or deletion, any previous and operation operations cannot be redone.

Asked in the 25-minute coding assessment stage. Implement text_editor(operations), where each operation is a dictionary: {"type":"insert","text":"..."}, {"type":"delete","n":k}, {"type":"undo"}, or {"type":"redo"}. Return the final text. A new insertion or deletion clears the redo history. Example: insert abc, delete 1, undo returns abc; redo returns ab. Constraints: at most 10^5 operations, and deletion removes up to n existing characters.

Constraints

  • 1 <= len(operations) <= 10^5
  • Each inserted text is a string of length at most 10^4
  • Each delete operation has an integer n where 1 <= n <= 10^9
  • Deletion removes up to n characters currently present
  • Undo and redo do nothing when their corresponding stack is empty

Function Signature

def text_editor(operations):
Interviewer

Your question is Text Editor Data Structure Exercise. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.