KatInterview

Collapse Adjacent Edits Before Applying Them to a Text Buffer

Stacks & QueuesAuto-graded · 21 test cases
MediumAuto-gradedStacks & Queuesstacksimulationcommand-pattern

Last updated

Simulate a queue of insert and format edits, merging adjacent inserts and dropping repeated adjacent formats, then return the final text and ordered list of applied styles.

Edit Queue Compaction

A text editor keeps a buffer that starts with some initial text. A queue of edits is applied to it in order. Before applying anything, the editor compacts the queue to remove redundant work.

Each edit is one of two kinds:

  • "insert": appends the given string to the end of the buffer text.
  • "format": records that the given style name (for example "BOLD") was applied.

The edits are supplied as two parallel lists. operations[i] is the kind of the i-th edit, and arguments[i] is a list whose first element is that edit's string argument (the text to insert, or the style name).

Compaction rules

Process the edits from left to right, comparing each incoming edit with the last edit that has been kept so far (not the raw previous input):

  1. If an insert arrives and the last kept edit is also an insert, merge them into one insert whose text is the concatenation of the two (earlier text first).
  2. If a format arrives and the last kept edit is a format with exactly the same style string (case-sensitive), discard the incoming one.
  3. Otherwise, keep the edit as a new entry.

Because comparison is against the last kept edit, chains collapse fully: three inserts in a row become one, and three identical formats in a row become one. Identical formats that are separated by any other kept edit (including an insert of an empty string) are not adjacent and are both kept.

Applying the compacted queue

Apply the kept edits in order, starting from initial_text:

  • an insert appends its text to the buffer;
  • a format appends its style to a list of applied styles.

Task

Return a dictionary with two keys:

  • "text": the final buffer text,
  • "formats": the list of applied styles, in the order they were applied.

Special cases

  • If an operation name is neither "insert" nor "format", raise ValueError.
  • If operations and arguments have different lengths, only the first min(len) pairs are considered.
  • With no edits, return initial_text and an empty list.
  • The input lists must not be modified.

Constraints

  • Up to 10,000 edits.
  • Each arguments[i] contains at least one string; only its first element is used.
  • initial_text, inserted texts and style names are strings (they may be empty).
  • Edit order must never be rearranged.

Examples

Example 1

Input: compact_and_apply_edits('Hello', ['insert', 'format', 'format', 'insert'], [[' World'], ['BOLD'], ['BOLD'], ['!']])

Output: {'text': 'Hello World!', 'formats': ['BOLD']}

Example 2

Input: compact_and_apply_edits('', ['insert', 'insert', 'format'], [['a'], ['b'], ['ITALIC']])

Output: {'text': 'ab', 'formats': ['ITALIC']}

Example 3

Input: compact_and_apply_edits('Draft', ['format', 'format', 'format', 'insert'], [['BOLD'], ['ITALIC'], ['BOLD'], ['!']])

Output: {'text': 'Draft!', 'formats': ['BOLD', 'ITALIC', 'BOLD']}

Hints

Hint 1

Scan the edits once, keeping a list of retained edits; only the last retained edit matters for each decision.

Hint 2

Compare the incoming edit with the last retained edit, not with the previous raw input. This makes runs of three or more collapse correctly.

Hint 3

Equal formats separated by an insert (even an empty one) are not adjacent. After compaction, run through the kept edits to build the text and the style list.