KatInterview

Resolve Overlapping Text Edit Spans by Priority

Greedy & IntervalsAuto-graded · 17 test cases
MediumAuto-gradedGreedy & Intervalsintervalssweep-lineheap

Last updated

Sweep over span boundaries with a heap to resolve overlapping inclusive edit spans using priority, original length, and input order, splitting losers into fragments.

Resolve Overlapping Text Edit Spans by Priority

You are given a list of edit records for a piece of text. Each record is a dictionary with these keys:

  • start (int): first index covered, inclusive
  • end (int): last index covered, inclusive
  • priority (int): a larger number means a stronger edit
  • change (string): the kind of edit, for example "DELETE", "DEDUP", "UPDATE", "UPPERCASE" or "LOWERCASE"

Records may overlap. Produce a list of records that do not overlap, in which every index is assigned to exactly one winning record, determined as follows.

Choosing the winner at an index

If several records cover the same index, the winner is picked by these rules, in order:

  1. The record with the larger priority wins.
  2. If priorities are equal, the record with the smaller original length end - start wins.
  3. If still tied, the record that appears earlier in the input list wins.

Splitting

A record that loses on only part of its range is cut into the pieces on which it still wins. Every piece is a separate output record that carries the same priority, change and any other fields as the original record, with only start and end changed.

The rules above always use the original record's priority, length and input position, even for a piece. For example, if (1, 10) is split into (1, 2) and (7, 10), both pieces are still treated as having length 10 - 1 = 9 when compared with anything else.

Output

Return the output records sorted by start. The output must be minimal:

  • Indices covered by no input record produce no output.
  • Two consecutive output records are merged into one if they come from the same original record and are directly adjacent (the second starts at the first's end + 1). Adjacent records that come from different input records are never merged, even if all their fields are equal.
  • Every output record keeps all fields of its source record (including any extra keys), with start and end replaced by the piece's bounds.

The input can be large, so the solution must not loop over every individual index.

Constraints

  • 0 <= len(corrections); the list may be empty, in which case return [].
  • start <= end for every record; a record with start == end covers a single index.
  • Indices and priorities are integers and may be large in magnitude.
  • The input list must not be relied upon to be sorted.
  • The time limit expects roughly O(n log n) behavior.

Examples

Example 1

Input: resolve_edit_spans([{'start': 1, 'end': 10, 'priority': 1, 'change': 'UPPERCASE'}, {'start': 3, 'end': 6, 'priority': 1, 'change': 'LOWERCASE'}])

Output: [{'start': 1, 'end': 2, 'priority': 1, 'change': 'UPPERCASE'}, {'start': 3, 'end': 6, 'priority': 1, 'change': 'LOWERCASE'}, {'start': 7, 'end': 10, 'priority': 1, 'change': 'UPPERCASE'}]

Example 2

Input: resolve_edit_spans([{'start': 0, 'end': 4, 'priority': 1, 'change': 'DELETE'}, {'start': 2, 'end': 6, 'priority': 3, 'change': 'UPDATE'}])

Output: [{'start': 0, 'end': 1, 'priority': 1, 'change': 'DELETE'}, {'start': 2, 'end': 6, 'priority': 3, 'change': 'UPDATE'}]

Example 3

Input: resolve_edit_spans([{'start': 5, 'end': 8, 'priority': 2, 'change': 'UPPERCASE'}, {'start': 5, 'end': 8, 'priority': 2, 'change': 'LOWERCASE'}])

Output: [{'start': 5, 'end': 8, 'priority': 2, 'change': 'UPPERCASE'}]

Hints

Hint 1

The winner at an index can only change at a record's start or just after its end (end + 1). Collect those boundary points and sort them.

Hint 2

Between two consecutive boundary points the set of covering records is constant, so one record wins the whole stretch. Keep the active records in a heap keyed by (-priority, end - start, input_index), computed once from the original record.

Hint 3

Remove expired records lazily when they reach the top of the heap (end is less than the current point). To keep output minimal, extend the previous output piece when the same record wins a directly adjacent stretch, and reset that state across gaps.