Skip to main content

243. Shortest Word Distance

easyAsked at LinkedIn

Given an array of strings and two words, return the shortest distance between two indices where these words appear. LinkedIn asks this as the entry point to their signature Shortest Word Distance family — they want the single-pass O(n) solution with two index trackers.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in LinkedIn loops.

  • Glassdoor (2026-Q1)LinkedIn SWE phone-screen reports list this and its II/III variants as the highest-frequency LinkedIn-specific question.
  • Blind (2025-12)LinkedIn writeups specifically call out the Shortest Word Distance series as a LinkedIn signature.

Problem

Given an array of strings wordsDict and two different strings that already exist in the array word1 and word2, return the shortest distance between these two words in the list.

Constraints

  • 2 <= wordsDict.length <= 3 * 10^4
  • 1 <= wordsDict[i].length <= 10
  • wordsDict[i] consists of lowercase English letters.
  • word1 and word2 are in wordsDict.
  • word1 != word2

Examples

Example 1

Input
wordsDict = ["practice", "makes", "perfect", "coding", "makes"], word1 = "coding", word2 = "practice"
Output
3

Example 2

Input
wordsDict = ["practice", "makes", "perfect", "coding", "makes"], word1 = "makes", word2 = "coding"
Output
1

Approaches

1. Two-pass: collect indices then min-distance

Walk once to collect index lists for each word. Then double-loop to find the minimum |i - j| across the two lists.

Time
O(n + a * b) where a, b are counts of the two words
Space
O(a + b)
function shortestDistanceTwoPass(wordsDict, word1, word2) {
  const idx1 = [], idx2 = [];
  for (let i = 0; i < wordsDict.length; i++) {
    if (wordsDict[i] === word1) idx1.push(i);
    else if (wordsDict[i] === word2) idx2.push(i);
  }
  let best = Infinity;
  for (const a of idx1) for (const b of idx2) best = Math.min(best, Math.abs(a - b));
  return best;
}

Tradeoff: Worst case O(n^2 / 4) when both words are common. Mention it then move to the linear version.

2. Single-pass with two index trackers (optimal)

Walk once. Maintain the most-recent index of word1 (i1) and word2 (i2). Whenever both are set, compute |i1 - i2| and track the min.

Time
O(n)
Space
O(1)
function shortestDistance(wordsDict, word1, word2) {
  let i1 = -1, i2 = -1, best = Infinity;
  for (let i = 0; i < wordsDict.length; i++) {
    if (wordsDict[i] === word1) i1 = i;
    else if (wordsDict[i] === word2) i2 = i;
    if (i1 !== -1 && i2 !== -1) best = Math.min(best, Math.abs(i1 - i2));
  }
  return best;
}

Tradeoff: Linear time, O(1) space. The invariant: at every position, i1 and i2 are the most-recent indices of their respective words — and the closest pair always involves the most-recent occurrence on at least one side.

LinkedIn-specific tips

LinkedIn interviewers expect the single-pass version directly. The key insight to articulate: 'I only need the most-recent index of each word — any earlier occurrence is dominated by the more recent one.' Be ready for the II (with multiple queries) and III (word1 might equal word2) follow-ups; LinkedIn loops routinely chain all three.

Common mistakes

  • Computing the min only when the current word is a hit instead of after every iteration where both indices are valid.
  • Using <= instead of === when comparing words (string comparison gotcha in some languages, not JS).
  • Forgetting to initialize i1 and i2 to a sentinel like -1 — gives wrong answers on the first iteration.

Follow-up questions

An interviewer at LinkedIn may pivot to one of these next:

  • Shortest Word Distance II (LC 244) — many queries on the same list, so precompute.
  • Shortest Word Distance III (LC 245) — word1 and word2 may be the same.
  • What if the list were streaming? (Same algorithm — i1, i2 just need to update on each token.)

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why don't we need to track all occurrences?

Because the minimum distance always involves the MOST RECENT occurrence of at least one word. If you have an older index for word1 and a current word2, the distance to the older word1 is greater than the distance to the most-recent word1 — so the older one can be safely discarded.

How does this generalize to the II variant?

Precompute index lists per word at construction time (O(n)). Per query, use the merge-like two-pointer walk on the two sorted index lists to find the minimum distance in O(a + b).

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

Drill Shortest Word Distance and other LinkedIn interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →