KatInterview

Cluster Interchangeable Words by Surrounding Context

Trees & GraphsAuto-graded · 26 test cases
MediumAuto-gradedTrees & Graphsunion-findhashingstrings

Last updated

Tests context hashing, union-find for transitive closure, and precise output formatting to group words that are mutually substitutable in identical four-word windows.

Problem

You have a collection of sentences. A word's frame is the pair of words immediately to its left and the pair of words immediately to its right — a 4-tuple (left2, left1, right1, right2). A word only has a frame if there are at least two words on each side within the same sentence.

Two distinct words are interchangeable when at least one occurrence of each appears in the same frame. Interchangeability is transitive: if X is interchangeable with Y, and Y is interchangeable with Z, then X and Z belong to the same cluster, even if they never share a frame directly.

Return every cluster of interchangeable words. Words with no interchangeable partner are not reported.

Rules

  • Split each sentence on single spaces and number the words 0, 1, 2, … within that sentence.
  • Word at position i has a frame only when i >= 2 and i <= len(words) - 3.
  • A word is identified by its exact character sequence; every occurrence of the same string is the same word.
  • Occurrences in different sentences are compared against each other using the same global frame table.
  • Within each cluster, sort the words in ascending lexicographic order.
  • Sort the list of clusters by each cluster's first (lexicographically smallest) word.
  • Return an empty list when no clusters exist.

Constraints

  • 1 <= len(sentences) <= 10^4
  • Each sentence contains between 1 and 100 words; all sentences together contain at most 2 * 10^5 words.
  • Every word is 1–20 lowercase English letters with no punctuation.
  • Sentences have no leading or trailing spaces and words are separated by exactly one space.

Examples

Example 1

Input: find_interchangeable_clusters(['i really like the movie a lot', 'i really love the movie a lot', 'we really love the show very much', 'we really enjoy the show very much'])

Output: [['enjoy', 'like', 'love']]

Example 2

Input: find_interchangeable_clusters(['p q red r s', 'p q blue r s', 'm n big o t', 'm n large o t', 'm n huge o u'])

Output: [['big', 'large'], ['blue', 'red']]

Example 3

Input: find_interchangeable_clusters(['the cat sat'])

Output: []

Hints

Hint 1

For each valid position, build a 4-tuple of the surrounding words and think of it as a bucket — all words that land in the same bucket are directly interchangeable. How can you avoid comparing every pair inside a large bucket?

Hint 2

You only need one representative per bucket: record the first word seen for each frame. Every later word in that same frame gets linked to that representative, keeping the work per position O(1) amortized.

Hint 3

Once you have edges between words, the clusters are just connected components. Union-Find with path compression handles the transitive closure efficiently and lets you collect components at the end by grouping every registered word with its root.