Merge Two Insertion Diffs
Asked at Jane Street
Reported by the problem's creator, not verified
An insertion diff is a list of [position, value] pairs, sorted by position, with no position repeated. Applying a diff to a document places each value at index position of the result; the document's own elements keep their relative order and occupy the remaining indexes from left to right. For example, applying [[0, "a"], [3, "b"]] to ["x", "y", "z"] produces ["a", "x", "y", "b", "z"].
The document is not part of the input and may be very long, so it cannot be built explicitly. diff_one is applied first. The positions in diff_two are indices in the document after both diffs have been applied, i.e. final positions.
Examples
Example 1
Input: diff_one =
[[0,"a"],[3,"b"]], diff_two =[[0,"c"],[4,"d"]]Output:
[[0,"c"],[1,"a"],[4,"d"],[5,"b"]]Explanation: After
diff_one,asits at index 0 andbat index 3.diff_twoputscat final index 0 anddat final index 4.ctakes index 0 and pushesato 1;canddboth land at or beforeb, pushing it to 5.