Process document edit events
Document Editor Event Processor
Implement a line-based document editor that processes a series of edit events.
You will be provided with:
- An initial document, represented as an array of strings, where each string corresponds to a single line of text.
- A chronologically ordered list of events to apply to the document.
Each event in the list will be one of the following types:
{"type": "insert_line", "index": i, "text": s}: This event instructs to insert a new linesat the 0-based positioniwithin the document. Valid indices for insertion range from0up to, and including, the currentdocument.length.{"type": "delete_document"}: This event instructs to completely clear the document, making it an empty array of strings.
Your task is to apply all events in the given order and return the final state of the document as an array of strings. Pay close attention to various corner cases, such as inserting into an empty document, inserting at the very beginning or end of the document, handling multiple consecutive delete_document events, and processing subsequent edits after the document has been entirely deleted.
AI interview prep
Core dilemma
Balancing the simplicity and cache efficiency of standard dynamic arrays against the performance overhead of O(N) insertions/deletions for large documents and high event volumes.
2 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Process document edit events
Document Editor Event Processor
Implement a line-based document editor that processes a series of edit events.
You will be provided with:
- An initial document, represented as an array of strings, where each string corresponds to a single line of text.
- A chronologically ordered list of events to apply to the document.
Each event in the list will be one of the following types:
{"type": "insert_line", "index": i, "text": s}: This event instructs to insert a new linesat the 0-based positioniwithin the document. Valid indices for insertion range from0up to, and including, the currentdocument.length.{"type": "delete_document"}: This event instructs to completely clear the document, making it an empty array of strings.
Your task is to apply all events in the given order and return the final state of the document as an array of strings. Pay close attention to various corner cases, such as inserting into an empty document, inserting at the very beginning or end of the document, handling multiple consecutive delete_document events, and processing subsequent edits after the document has been entirely deleted.