Word Lookup in a Large Lexicon
StringsAuto-graded · 18 test casesLast 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 wordssearch_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
lexiconare 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
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is recognizing this as a membership testing problem that requires efficient lookups across many queries. Scanning the lexicon linearly for each search term would cost O(N × Q) where N is the lexicon size and Q is the number of search terms—with N up to 1,000,000 and Q up to 100,000, this becomes prohibitively expensive.
Instead, we preprocess the lexicon into a hash-based set once, then perform constant-time lookups for each search term:
- Build a hash set from the lexicon. This converts the list into a structure optimized for membership checks.
- Query the set for each search term, collecting the boolean results in order.
Why This Works
A Python set uses hash tables internally. When we check term in word_set, Python:
- Computes a hash of the search term (O(L) where L is string length)
- Uses the hash to locate potential matches in the table
- Performs equality comparison if needed (O(L) for strings)
The expected time per lookup is O(L), making it vastly faster than scanning all N words. The set stores references to existing string objects rather than copying them, keeping memory overhead modest.
Since matching is exact and case-sensitive by design, no normalization or transformation is needed—we check for literal presence in the lexicon.
Complexity Analysis
Let N = number of words in lexicon, Q = number of search terms, and L = average string length.
Time Complexity: O(N×L + Q×L) expected
- Building the set: O(N×L) to hash each lexicon word
- Processing queries: O(Q×L) for Q lookups, each taking O(L) expected time
- Overall: linear in the total input size, which is optimal since we must examine every character
Space Complexity: O(N)
- The hash set stores N references plus hash table overhead
- Output list requires O(Q) space
- Total auxiliary space is O(N), as the set is the dominant structure
Solution
from typing import List
def word_lookup(lexicon: List[str], search_terms: List[str]) -> List[bool]:
word_set = set(lexicon)
return [term in word_set for term in search_terms]