Dictionary Lookup Allowing One Mismatched Character in Equal-Length Words
Company: Meta
Role: Applied Scientist
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Overview: Given a dictionary of lowercase words and many queries, report for each query whether some dictionary word of the same length differs from it in at most one position. Tests going beyond a pairwise scan so that the lookup scales to large dictionaries and large batches of queries.
Read the full Meta Applied Scientist interview experience this question came from
Constraints
- 1 <= len(words) <= 10^5
- 1 <= len(queries) <= 10^5
- Every word and every query has length between 1 and 20 inclusive.
- Words and queries contain only lowercase English letters a to z.
Examples
Input: (['hello', 'world', 'help'], ['hallo', 'hello', 'helps', 'word', 'wprld', 'hekko'])
Expected Output: [True, True, False, False, True, False]
Explanation: Source example 1: hallo and wprld differ from hello and world at one index, hello matches exactly, helps and hekko differ from hello at two indices, and help differs from word at all four indices.
Input: (['a'], ['b', 'a', 'ab'])
Expected Output: [True, True, False]
Explanation: Source example 2: b differs from a at one index, a matches exactly, and ab has length 2 while the only word has length 1.
Hints
- A query can only match dictionary words of exactly its length; an inserted or deleted character never matches.
- Comparing every query with every word can take about 10^10 string comparisons at the maximum input. Every string has at most 20 characters, so look for work that grows with the string length rather than with the dictionary size.
- An exact match counts as a match, and duplicate dictionary words never change any answer.