Measure the Closest Occurrences of Two Different Words

Quick Overview

Measure the Closest Occurrences of Two Different Words. Given wordsDict and two distinct words occurring in it, return the minimum absolute difference between an index of word1 and an index of word2. Indices are zero

Measure the Closest Occurrences of Two Different Words

Company: Snowflake

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Given wordsDict and two distinct words occurring in it, return the minimum absolute difference between an index of word1 and an index of word2. Indices are zero-based. ```cpp int shortestDistance(vector<string>& wordsDict, string word1, string word2); ``` ### Examples ```text wordsDict = ["red","blue","red","green","blue"], word1="red", word2="blue" -> 1 wordsDict = ["one","x","two"], word1="one", word2="two" -> 2 ``` ### Constraints - Dictionary length is 2 through 30000. - Every word and target has 1 through 10 lowercase English letters. - Targets differ and both are present.

Overview: Measure the Closest Occurrences of Two Different Words. Given wordsDict and two distinct words occurring in it, return the minimum absolute difference between an index of word1 and an index of word2. Indices are zero

Snowflake logo
Snowflake
Source window: 0–6 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
1
0

Problem

Given wordsDict and two distinct words occurring in it, return the minimum absolute difference between an index of word1 and an index of word2. Indices are zero-based.

int shortestDistance(vector<string>& wordsDict, string word1, string word2);

Examples

wordsDict = ["red","blue","red","green","blue"], word1="red", word2="blue" -> 1
wordsDict = ["one","x","two"], word1="one", word2="two" -> 2

Constraints

  • Dictionary length is 2 through 30000.
  • Every word and target has 1 through 10 lowercase English letters.
  • Targets differ and both are present.