Compress editor keystrokes into insert/delete/skip
Company: Replit
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Onsite
Quick Answer: This question evaluates simulation of sequential editor operations and compression logic, assessing skills in string manipulation, state tracking, and sequence transformation within the Coding & Algorithms domain.
Constraints
- 0 <= len(doc) <= 2 * 10^5
- 0 <= len(operations) <= 2 * 10^5
- Each append operation uses a single-character string
- Expected solution should run in O(len(doc) + len(operations)) time
Examples
Input: ("", [("append", "a"), ("append", "b"), ("append", "c")])
Expected Output: [("insert", "abc")]
Explanation: Three consecutive appends merge into one insert.
Input: ("aa", [("right",), ("backspace",)])
Expected Output: [("delete", 1)]
Explanation: The first original character is crossed and then removed, so it becomes one delete.
Input: ("aa", [("right",)])
Expected Output: [("skip", 1)]
Explanation: One original character is kept and the cursor ends after it.
Input: ("", [("append", "x"), ("backspace",), ("backspace",), ("right",)])
Expected Output: []
Explanation: The append is canceled by backspace, and the remaining operations do nothing on an empty document.
Input: ("abcde", [("right",), ("right",), ("append", "X"), ("right",), ("backspace",), ("append", "Y")])
Expected Output: [("skip", 2), ("insert", "XY"), ("delete", 1)]
Explanation: Keep `a` and `b`, insert `XY`, and delete `c`. The suffix `de` stays untouched.
Input: ("ab", [("append", "x"), ("right",), ("backspace",)])
Expected Output: [("insert", "x"), ("delete", 1)]
Explanation: Insert `x` before the document, then the crossed original `a` is removed.
Input: ("abcd", [("right",), ("right",), ("backspace",), ("backspace",)])
Expected Output: [("delete", 2)]
Explanation: Both crossed original characters are deleted, so the result is one delete of length 2.
Input: ("abcd", [("right",), ("append", "x"), ("right",), ("backspace",), ("append", "y"), ("right",), ("append", "z"), ("right",), ("right",)])
Expected Output: [("skip", 1), ("insert", "xy"), ("delete", 1), ("skip", 1), ("insert", "z"), ("skip", 1)]
Explanation: Keep `a`, insert `xy`, delete `b`, keep `c`, insert `z`, keep `d`. The final extra right is ignored because the cursor is already at the end.
Hints
- Simulate the editor as two parts: content to the left of the cursor and the untouched suffix of the original document. `right()` only moves the next original character into the left side.
- After simulation, every original character in the processed prefix is either still present before the cursor (`skip`) or was crossed and later removed (`delete`). Surviving inserted characters appear between them and become `insert` operations.