FastPrepRepeated Shortest Word Distance Queries
Problems/Repeated Shortest Word Distance Queries

Repeated Shortest Word Distance Queries

LinkedIn logoLinkedIn● MediumFULLTIMENEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a LinkedIn phone screen. The task is the shortest word distance, asked many times. Here, you get a finite batch of queries, and we chose the limits. Our version is about 99% the same as the reported question.

You are given an array words and a batch of queries. Each query contains two distinct words that both appear in words.

For each query [first, second], return the minimum absolute difference between an index containing first and an index containing second.

Preprocess words once, then answer every query. Return the distances in query order.

Function

shortestWordDistances(words: String[], queries: String[][]) → int[]

Examples

Example 1

words = ["practice","makes","perfect","coding","makes"]queries = [["coding","practice"],["makes","coding"],["practice","makes"]]return = [3,1,1]

coding and practice occur at indices 3 and 0. The closest makes to coding is at index 4, and the closest makes to practice is at index 1.

Example 2

words = ["a","b","a","c","b","a"]queries = [["a","b"],["a","c"],["b","c"]]return = [1,1,1]

Each queried pair has adjacent occurrences somewhere in the array, so every minimum distance is 1.

Example 3

words = ["red","blue","green","yellow","red","green"]queries = [["blue","yellow"],["red","green"],["yellow","red"]]return = [2,1,1]

The only blue and yellow positions differ by 2. The later red is adjacent to both green at index 5 and yellow at index 3.

Constraints

  • 2 ≤ words.length ≤ 200000.
  • 1 ≤ queries.length ≤ 200000.
  • Every word contains 1 to 40 lowercase English letters.
  • Each query contains exactly two distinct words, and both words occur in words.
  • The total number of characters across words and queries is at most 2 * 10^6.

More LinkedIn Phone Screen questions

  • Maximum Product of a Contiguous SubarrayPHONE SCREEN · Seen Nov 2026
  • Maximum Sum of a Contiguous SubarrayPHONE SCREEN · Seen Nov 2026
  • Merge Two Sorted ListsPHONE SCREEN · Seen Nov 2026
  • Sqrt(x)PHONE SCREEN · Seen Nov 2026
  • Total Years of Work ExperiencePHONE SCREEN · Seen Nov 2026
  • Sum an Arbitrary-Dimensional ArrayPHONE SCREEN · Seen Oct 2026
  • Known Words Matching Phone DigitsPHONE SCREEN · Seen Oct 2026
  • Inverse-Depth Nested List SumPHONE SCREEN · Seen Oct 2026
  • Valid Number with ExponentsPHONE SCREEN · Seen Oct 2026
  • Sort a Quadratic-Transformed ArrayPHONE SCREEN · Seen Oct 2026

Similar questions at other companies

  • Merge Keyed 2D ArraysWhatNot · PHONE SCREENWhatNot interview questions
  • Disjoint Target-Sum TripletsAmerican Express · PHONE SCREENAmerican Express interview questions
  • Sparse Vector Dot ProductAnduril · PHONE SCREENAnduril interview questions
  • Unique Pairs in a 2D Matrix Summing to TargetAmazon · PHONE SCREEN
  • Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
  • Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
  • Find Two-Sum or Three-Sum Index CombinationsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Efficient TeamsExpedia · OAExpedia interview questions
  • Sparse Vector Dot Product from TuplesMeta · ONSITE INTERVIEWMeta interview questions
  • Count Cross-Array Target-Sum PairsVisa · OAVisa interview questions

LinkedIn interview experiences

  • LinkedIn Senior MLE Interview Questions: 1 Experience
  • LinkedIn Staff AI Engineer Phone Screens: 1 Experience
  • LinkedIn Experienced MLE Onsite: 1 Experience
  • LinkedIn Coding with AI Interview: 4 Experiences
  • LinkedIn Experienced Infrastructure SWE Onsite: 1 Experience
All LinkedIn interview questionsSee LinkedIn hiring insights
Saved
class Solution {
    public int[] shortestWordDistances(String[] words, String[][] queries) {
        // Write your code here.
    }
}
Checking account…