Shortest Word Distance
Updated
Problem statement
You are given a list of words wordsDict and two different words, word1 and word2. Both words appear in the list at least once.
The distance between the word at index i and the word at index j is |i - j|. Return the smallest distance between any position that holds word1 and any position that holds word2.
A position holds a word only when the whole word is equal to it. A longer word that starts with or contains word1 does not count. Either word may come first in the list.
Function
shortestDistance(wordsDict: String[], word1: String, word2: String) → intExamples
Example 1
wordsDict = ["bond","yield","price","bond","rate","yield"]word1 = "rate"word2 = "bond"return = 1"rate" is at index 4. "bond" is at indexes 0 and 3.
The distances are 4 - 0 = 4 and 4 - 3 = 1. The smallest is 1.
Example 2
wordsDict = ["ask","bid","fill","ask","bid","ask"]word1 = "bid"word2 = "ask"return = 1"bid" is at indexes 1 and 4. "ask" is at indexes 0, 3 and 5.
Every "bid" has an "ask" right next to it, so the smallest distance is 1. For example, index 1 and index 0 are 1 apart. The "ask" may come before or after the "bid".
Example 3
wordsDict = ["tick","ticker","quote","news","tick"]word1 = "tick"word2 = "quote"return = 2"tick" is at indexes 0 and 4. "quote" is at index 2.
"ticker" at index 1 is a different word, even though it starts with "tick", so it does not count. The distances are 2 and 2, so the answer is 2.
Constraints
2 <= wordsDict.length <= 100001 <= wordsDict[i].length <= 101 <= word1.length, word2.length <= 10wordsDict[i],word1andword2contain only lowercase English letters.word1andword2both appear inwordsDict, andword1 != word2.
More Bloomberg LP Onsite Interview questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Replacements to Make Two Strings AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Currency Conversion RatePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Longest Common PrefixKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Bounded Word Guess SolverAtlassian · ONSITE INTERVIEWAtlassian interview questions
- Filter Pins by Excluded IDsPinterest · ONSITE INTERVIEWPinterest interview questions
- Longest Common Parent DirectoryGoogle · ONSITE INTERVIEWGoogle interview questions
- K-Second Message SuppressionMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Word Search Across DocumentsConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
- Retry with an Attempt LimitOracle · ONSITE INTERVIEWOracle interview questions
- Extract Error Log ContextFlexTrade · ONSITE INTERVIEWFlexTrade interview questions
- Compress Consecutive Integer RangesDocuSign · ONSITE INTERVIEWDocuSign interview questions
Interview reports that mention this question
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Experienced Design & More: 126 Questions (Part 2 of 2)
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)