Shortest Word Distance Queries

Quick Overview

Preprocess a fixed word sequence so repeated word-pair queries return their exact minimum index distance without rescanning the full input.

Shortest Word Distance Queries

Company: LinkedIn

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

# Shortest Word Distance Queries Implement `shortest_word_distances(words: list[str], queries: list[list[str]]) -> list[int]`. Preprocess the fixed word sequence, then answer each query `[first, second]` with the minimum absolute difference between an index containing `first` and an index containing `second`. ### Input Domain - `1 <= len(words) <= 200,000`. - `0 <= len(queries) <= 200,000`. - Words are nonempty ASCII strings. - Every query contains exactly two distinct words that both occur in `words`. ### Output Rules - Sequence indices are zero-based when distances are computed. - Different occurrences of a word are separate candidates. - Return one exact minimum distance per query in query order. - Repeated queries are allowed. ### Constraints - Preprocessing should be linear in `len(words)`. - A query should inspect only occurrence positions for its two words, not the full sequence. ### Examples #### Example 1 Input: `words = ["practice","makes","perfect","coding","makes"], queries = [["coding","practice"],["makes","coding"]]` Output: `[3,1]` #### Example 2 Input: `words = ["a","b","a","c","b"], queries = [["a","b"],["a","c"]]` Output: `[1,1]` ```hint Merge two sorted occurrence lists Store each word's indices in increasing order, then advance the pointer at the smaller current index while tracking the best gap. ```

Quick Answer: Preprocess a fixed word sequence so repeated word-pair queries return their exact minimum index distance without rescanning the full input.

|Home/Coding & Algorithms/LinkedIn
LinkedIn logo
LinkedIn
Sep 1, 2026
hardSoftware EngineerTechnical ScreenCoding & Algorithms
5
0

Shortest Word Distance Queries

Implement shortest_word_distances(words: list[str], queries: list[list[str]]) -> list[int].

Preprocess the fixed word sequence, then answer each query [first, second] with the minimum absolute difference between an index containing first and an index containing second.

Input Domain

  • 1 <= len(words) <= 200,000 .
  • 0 <= len(queries) <= 200,000 .
  • Words are nonempty ASCII strings.
  • Every query contains exactly two distinct words that both occur in words .

Output Rules

  • Sequence indices are zero-based when distances are computed.
  • Different occurrences of a word are separate candidates.
  • Return one exact minimum distance per query in query order.
  • Repeated queries are allowed.

Constraints

  • Preprocessing should be linear in len(words) .
  • A query should inspect only occurrence positions for its two words, not the full sequence.

Examples

Example 1

Input: words = ["practice","makes","perfect","coding","makes"], queries = [["coding","practice"],["makes","coding"]]

Output: [3,1]

Example 2

Input: words = ["a","b","a","c","b"], queries = [["a","b"],["a","c"]]

Output: [1,1]

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...