Nearest Vocabulary Word
Updated
Problem statement
Given a vocabulary array vocabulary and an input string word, return word when it appears in the vocabulary. Otherwise, return the vocabulary entry with the smallest Levenshtein edit distance from word.
One edit inserts, deletes, or replaces one character. Matching is case-sensitive. If several entries have the same minimum distance, return the lexicographically smallest one. Return the empty string when the vocabulary is empty.
Function
nearestVocabularyWord(vocabulary: String[], word: String) → StringExamples
Example 1
vocabulary = ["cat","bat","rat"]word = "hat"return = "bat"All three vocabulary words are one replacement away; bat is lexicographically smallest.
Example 2
vocabulary = ["dog","cat"]word = "dog"return = "dog"The input already belongs to the vocabulary, so it is returned unchanged.
Constraints
0 <= vocabulary.length <= 10000 <= vocabulary[i].length, word.length <= 100- All strings contain only lowercase English letters.
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Maximum Bouquet ValueOracle · ONSITE INTERVIEWOracle interview questions
- Word BreakAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Word BreakGoogle · ONSITE INTERVIEWGoogle interview questions
- Scramble StringExpedia · ONSITE INTERVIEWExpedia interview questions
- Word BreakMoveworks · ONSITE INTERVIEWMoveworks interview questions
- Validate a Palindrome After Limited DeletionsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Decode Numeric StringArcesium · ONSITE INTERVIEWArcesium interview questions
- Word BreakBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Count Vowel PermutationsZscaler · ONSITE INTERVIEWZscaler interview questions
- Minimum Steps on a Circular Character DialNavan · ONSITE INTERVIEWNavan interview questions