FastPrepShortest Word Distance
Problems/Shortest Word Distance

Shortest Word Distance

Bloomberg LP logoBloomberg LP● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A report from a Bloomberg software engineer loop in February 2025 names one coding question only by its number in a well-known public problem list, which is the classic task of finding how close two given words come in a list. The report gives no statement, limits or example. It says only that the loop also had a design discussion and questions about the candidate's own experience.

Everything else here is our practice choice. The two query words are different and both appear in the list. A word counts only when it is exactly equal to a query word, either word may come first, and the answer is the smallest index gap. The list has 2 to 10,000 lowercase words of 1 to 10 letters.

The judged task matches the report at about 85%.

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) → int

Examples

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 <= 10000
  • 1 <= wordsDict[i].length <= 10
  • 1 <= word1.length, word2.length <= 10
  • wordsDict[i], word1 and word2 contain only lowercase English letters.
  • word1 and word2 both appear in wordsDict, and word1 != 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 Interview Questions, Level Not Reported: 65 Questions

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)
All Bloomberg LP interview questionsSee Bloomberg LP hiring insights
Saved
class Solution {
    public int shortestDistance(String[] wordsDict, String word1, String word2) {
        // Write your code here.
    }
}
Checking account…