Document Edit Events
Updated
Problem statement
A document starts as the empty string. Process each event in order and return the final document.
["INSERT", position, text]insertstextbefore the zero-basedposition.["DELETE", position, length]removeslengthcharacters beginning atposition.
An event whose position or range is invalid for the current document is rejected and leaves the document unchanged.
Function
applyDocumentEdits(events: String[][]) → StringExamples
Example 1
events = [["INSERT","0","hello"],["INSERT","5"," world"],["DELETE","5","1"]]return = "helloworld"The second event adds a leading space before world, and the delete removes that space.
Example 2
events = [["INSERT","0","abc"],["DELETE","4","1"],["INSERT","1","X"]]return = "aXbc"The out-of-range delete is rejected; the final insert succeeds.
Constraints
0 <= events.length <= 20000.- Inserted text and the final document together contain at most 200000 ASCII characters.
- Numeric fields are nonnegative decimal integers.
Similar questions at other companies
- Christmas Tree with N TiersVerily · ONSITE INTERVIEW
- Bounded Word Guess SolverAtlassian · ONSITE INTERVIEWAtlassian interview questions
- Predict Runtime Method DispatchAMD · ONSITE INTERVIEWAMD interview questions
- Basic String Compression with Length GuardNew Relic · PHONE SCREEN · ONSITE INTERVIEW
- Print Sentences as TableAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
- Retry with an Attempt LimitOracle · ONSITE INTERVIEWOracle interview questions
- Extract Error Log ContextFlexTrade · ONSITE INTERVIEWFlexTrade interview questions
- Run-Length Encode Consecutive Letter RunsMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
- String Pattern ReplacementGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- IPv4 Forward IteratorOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions