KatInterview

Live String Index Store with Closest-Match Queries

Data Structure DesignAuto-graded · 17 test cases
HardAuto-gradedData Structure Designordered-settreapbinary-search-tree

Last updated

Tests ability to design a data structure that supports streaming character appends, point updates, and O(log N) nearest-occurrence queries over a mutable string.

Problem

You are building a live text buffer that grows and changes over time. The buffer starts with an initial string and must support three operations efficiently:

  1. Extend — push a single lowercase letter onto the end of the buffer.
  2. Overwrite — replace the letter at a given position with a new one.
  3. Closest occurrence — given a position and a target letter, find the smallest distance between that position and any occurrence of that letter anywhere in the buffer.

Implement the function live_char_store(s, operations, arguments) that processes a sequence of such operations against an initial string and returns the results.

Operations

OperationArgumentsReturns
"append"[ch]None
"update"[index, ch]None
"closest"[index, target]int
  • "append": add ch to the right end of the buffer. The new character is immediately visible to subsequent queries.
  • "update": set the character at position index to ch. If ch already equals the current character at that position, the buffer is unchanged.
  • "closest": return the minimum value of |index - j| over all positions j where the buffer currently holds target. Return -1 if target is not present anywhere in the buffer.

Constraints

  • 1 <= len(s) <= 10^5
  • ch and target are lowercase English letters ('a'–'z').
  • 0 <= index < current_buffer_length for every "update" and "closest" call.
  • At most 10^5 total operation calls across all three types combined.
  • The initial string contains only lowercase English letters.

Notes

  • The target letter may sit exactly at index, in which case the answer is 0.
  • When two occurrences of the target are equidistant from index (one to the left, one to the right), return that shared distance.
  • An "update" where the new character equals the existing one counts as a no-op and returns None without changing any internal state.

Examples

Example 1

Input: live_char_store('aabac', ['find_nearest', 'find_nearest', 'find_nearest', 'append', 'find_nearest', 'update', 'find_nearest'], [[2, 'a'], [2, 'c'], [0, 'z'], ['z'], [0, 'z'], [1, 'z'], [0, 'z']])

Output: [1, 2, -1, None, 5, None, 1]

Hints

Hint 1

For each of the 26 letters, track the set of positions where that letter currently appears. A query only ever needs to look at one of those 26 sets.

Hint 2

The closest occurrence is always either the largest stored position that is ≤ index (the predecessor) or the smallest stored position that is > index (the successor). You only need those two candidates.

Hint 3

Python has no built-in balanced BST, but you can implement a treap using flat arrays indexed by string position. Use a deterministic hash of the position as the heap priority so that sequentially appended indices still produce a balanced tree.