KatInterview

Word Lookup in a Large Lexicon

StringsAuto-graded · 18 test cases
MediumAuto-gradedStringshash-tablessetsoptimization

Last updated

Tests efficient membership checking using hash sets, understanding amortized preprocessing costs, and recognizing algorithmic trade-offs for large-scale lookups.

You are given a lexicon containing unique words and a collection of search terms. For each search term, determine whether it appears in the lexicon.

Write a function that accepts two parameters:

  • lexicon: a list of distinct strings representing the available words
  • search_terms: a list of strings to look up

Return a list of booleans, where each element indicates whether the corresponding search term exists in the lexicon.

Matching rules:

  • Comparisons are exact and case-sensitive: "Word" and "word" are different
  • Code-point matching is used: no Unicode normalization is applied
  • Each search term is evaluated independently; duplicate search terms receive separate results at their respective positions

Constraints:

  • 0 ≤ len(lexicon) ≤ 1,000,000
  • 0 ≤ len(search_terms) ≤ 100,000
  • All strings contain valid Unicode characters
  • 0 ≤ length of any string ≤ 1,000
  • All words in lexicon are distinct

Examples

Example 1

Input: word_lookup(['apple', 'banana', 'cherry'], ['banana', 'grape', 'apple'])

Output: [True, False, True]

Example 2

Input: word_lookup(['Reevo', 'dictionary', 'word'], ['reevo', 'word', ''])

Output: [False, True, False]

Hints

Hint 1

Building an index once is more efficient than scanning the entire lexicon for every search term

Hint 2

Python's set provides expected O(1) membership testing through hash-based lookups

Hint 3

Consider the total cost: preprocessing time plus per-query lookup time must both be reasonable