Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.