Merge Two Insertion Diffs

Asked at Jane StreetOnsite

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

  1. 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, a sits at index 0 and b at index 3. diff_two puts c at final index 0 and d at final index 4. c takes index 0 and pushes a to 1; c and d both land at or before b, pushing it to 5.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.