KatInterview

Build a Token Merger with Threshold-Based Pair Consolidation

StringsAuto-graded · 17 test cases
MediumAuto-gradedStringstokenizationgreedystate-management

Last updated

Tests implementation of a stateful tokenizer that learns merge operations by iteratively combining frequent adjacent pairs, then applies those rules to encode and decode text.

Build a stateful tokenizer that learns consolidation rules from text and applies them for encoding and decoding.

You will implement a function that processes a sequence of operations:

Operation: train

  • Arguments: text (string), threshold (integer)
  • Learns merge rules from the given text
  • Initially, each character is its own token
  • Repeatedly find all adjacent token pairs in the current sequence and count their occurrences
  • Select the pair with the highest count that appears at least threshold times
  • If multiple pairs tie for the highest count, choose the lexicographically smallest pair (comparing tuples of tokens)
  • Merge all non-overlapping occurrences of the selected pair from left to right into a single new token
  • Stop when no pair reaches the threshold
  • Returns: null

Operation: encode

  • Arguments: text (string)
  • Converts text into a list of token IDs using previously learned merge rules
  • Start with each character as a separate token
  • Apply all learned merges in the order they were learned
  • Map each resulting token to its integer ID
  • For characters not seen during training, use ID -1
  • Returns: list of integers

Operation: decode

  • Arguments: ids (list of integers)
  • Converts token IDs back to text
  • For each valid ID, retrieve its token and concatenate the characters
  • For invalid IDs (negative or out of range), use "?"
  • Returns: string

Token ID Assignment:

  • Base character tokens receive IDs in sorted lexicographic order starting from 0
  • Each newly created merged token receives the next available ID

Example Walkthrough:

For text = "abababa" with threshold = 2:

  • Initial tokens: [a, b, a, b, a, b, a]
  • Round 1: Pair (a, b) occurs 3 times, pair (b, a) occurs 2 times. Choose (a, b) lexicographically. Merge to get [ab, ab, ab, a]
  • Round 2: Pair (ab, ab) occurs 2 times. Merge to get [abab, ab, a]
  • Round 3: No pair occurs at least 2 times. Stop.

Constraints

  • 1 ≤ operations ≤ 100
  • 0 ≤ text length ≤ 10,000 per operation
  • 1 ≤ threshold ≤ text length
  • All text contains only printable ASCII characters
  • encode and decode are only called after at least one train
  • IDs in decode are integers in range [-1000, 1000]

Examples

Example 1

Input: process_tokenizer_operations(['train', 'encode', 'decode'], [['abababa', 2], ['abababa'], [[0, 1, 4]]])

Output: [None, [3, 2, 0], 'ab?']

Example 2

Input: process_tokenizer_operations(['train', 'encode', 'decode'], [['banana', 2], ['band'], [[0, 1, 999]]])

Output: [None, [1, 3, -1], 'ab?']

Hints

Hint 1

For pair counting, iterate through adjacent positions and track each (left_token, right_token) tuple. Overlapping occurrences like in 'aaa' should count the pair (a,a) twice at positions 0-1 and 1-2.

Hint 2

When merging the selected pair, scan from left to right and consume both tokens whenever you find a match. This ensures overlapping matches are resolved consistently by taking the leftmost one.

Hint 3

Apply merges during encoding in the exact order they were learned during training. Each merge pass should transform the entire sequence before the next merge is applied.