KatInterview

Closest Pair Gaps Between Words in a Fixed List

Data Structure DesignAuto-graded · 16 test cases
MediumAuto-gradedData Structure Designinverted-indexbinary-searchpreprocessing

Last updated

Answer many closest-index-distance queries over a fixed word list by building a position index, precomputing same-word gaps, and binary searching the smaller list.

Closest Pair Gaps Between Words in a Fixed List

You are given a fixed list of words, and a batch of queries is run against it. Each query names two words and asks how close together they appear in the list.

For a query (a, b), the answer is the smallest value of |i - j| over all index pairs where words[i] == a, words[j] == b, and i != j.

The two query words may be identical. In that case you need the smallest distance between two different positions holding that same word.

Function

Implement closest_word_gaps(words, ops, args_list):

  • words: the list of strings to search.
  • ops: a list of operation names.
  • args_list: a list the same length as ops. Entry k holds the arguments for ops[k].

Walk through ops and args_list in order. For each entry where ops[k] == "shortest", args_list[k] is a two-element list [a, b], and you must compute the answer for that query. Ignore every entry whose operation name is anything other than "shortest" (for example a constructor-style entry such as "WordDistance"); such entries produce no output.

Return a list of integers containing one answer per "shortest" entry, in the same order as the queries appear.

Notes

  • The distance is symmetric, so the order of a and b does not affect the answer.
  • Since the word list never changes between queries, work that depends only on words should be done once rather than repeated for every query.

Constraints

  • 2 <= len(words) <= 100,000
  • 1 <= len(words[i]) <= 10, and each word consists only of lowercase English letters.
  • Both query words are guaranteed to occur in words.
  • If a query uses the same word twice, that word is guaranteed to occur at least twice in words.
  • There are at most 50,000 "shortest" queries.
  • Scanning the whole list for every query will be too slow.

Examples

Example 1

Input: closest_word_gaps(['practice', 'makes', 'perfect', 'coding', 'makes'], ['shortest', 'shortest', 'shortest'], [['coding', 'practice'], ['makes', 'coding'], ['makes', 'makes']])

Output: [3, 1, 3]

Example 2

Input: closest_word_gaps(['a', 'c', 'b', 'a', 'a'], ['shortest', 'shortest'], [['a', 'b'], ['a', 'a']])

Output: [1, 1]

Hints

Hint 1

Because the list never changes, build something once up front: for each word, the sorted list of indices where it occurs.

Hint 2

For a query on the same word twice, the best pair is always two neighbouring entries in that word's index list, so the minimum neighbouring gap can be precomputed per word.

Hint 3

For two different words, loop over the shorter index list and, for each index, binary search the longer list. Only the first element at or after it and the element just before it can be the closest.