KatInterview

Longest Dictionary Word Ending With a Given Tail

Data Structure DesignAuto-graded · 22 test cases
MediumAuto-gradedData Structure Designtriestringssuffix

Last updated

Answer batches of suffix queries over a word list, returning the longest match with lexicographic tie-breaking, using a reversed-word trie with cached best words.

Suffix Lookup Service

You are given a dictionary of words and a batch of queries. Each query supplies a tail string. For each query, report the best dictionary word that ends with that tail.

Function inputs

  • words: the list of dictionary words. It may be empty, and it may contain duplicates (a duplicate behaves like a single copy of the word).
  • operations: a list of operation names, one per query, in order.
  • arguments: a list parallel to operations. Entry i is a list whose first element, arguments[i][0], is the tail string for query i. Any further elements are ignored.

Operation names

The only valid operation name is the exact string "find_best_word". If any entry of operations is a different string, the function must raise a ValueError and return nothing. The queries do not change the dictionary, so each query is answered independently against the full words list.

What to return

Return a list of strings with exactly one answer per query, in the same order as operations.

For a query with tail t, the candidates are all words w in words for which w.endswith(t) is true. Choose the answer as follows:

  1. If there are no candidates, the answer is the empty string "".
  2. Otherwise, the answer is the candidate with the greatest length.
  3. If several candidates share that greatest length, the answer is the one that is smallest in standard Python string ordering (< on str, which for lowercase letters is ordinary alphabetical order).

Further clarifications:

  • A word equal to the tail itself is a valid candidate.
  • A word shorter than the tail can never be a candidate.
  • The answer is always an actual word from words, returned exactly as it appears there.
  • A tail is matched only at the end of a word. A tail occurring elsewhere in the word does not count.
  • If words is empty, every answer is "".
  • If the tail is the empty string, every word ends with it, so the answer is the best word in the whole dictionary under rules 2 and 3 (or "" if the dictionary is empty).

Constraints

  • At most 50,000 words.
  • The total number of characters across all words is at most 1,000,000.
  • Each tail has length between 1 and 100 (the empty-tail rule above is defined for completeness).
  • Words and tails contain only lowercase English letters.
  • The same dictionary serves every query in the batch, so the solution should be efficient across many queries.

Examples

Example 1

Input: longest_word_with_tail(['apple', 'maple', 'le', 'banana', 'tale'], ['find_best_word', 'find_best_word'], [['le'], ['ana']])

Output: ['apple', 'banana']

Example 2

Input: longest_word_with_tail([], ['find_best_word', 'find_best_word'], [['x'], ['a']])

Output: ['', '']

Example 3

Input: longest_word_with_tail(['cat', 'dog', 'catalog', 'log'], ['find_best_word', 'find_best_word', 'find_best_word'], [['at'], ['og'], ['z']])

Output: ['cat', 'catalog', '']

Hints

Hint 1

A word ends with a tail exactly when the reversed tail is a prefix of the reversed word. Which data structure is built for prefix lookups?

Hint 2

Scanning every matching word for each query is too slow. Store at each trie node the best word among all words passing through it, so a query only reads one stored value.

Hint 3

Compare candidates by length first (longer wins), then alphabetically (smaller wins). Return an empty string when the walk falls off the trie, rather than the best of the partial path. Also validate operation names and raise ValueError for unknown ones.