Repeated Shortest Word Distance Queries
Reported by candidates from LinkedIn's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this LinkedIn OA, reported in September 2026, is rescanning the whole words array for every query. With 200000 words and 200000 queries, that's 4 * 10^10 steps and it dies on the big tests. The prompt even says it: preprocess once, then answer every query. This is a hash-table-of-index-lists problem with a two-pointer merge on top. If you blank on the merge in the live assessment, StealthCoder runs invisibly on your desktop as a safety net and gives you the working solution while you keep typing.
The problem
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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build a map from each word to a sorted list of its indices in one pass. Sorted comes free because you scan left to right. For each query, grab the two lists and walk them with two pointers. Compare the current indices, record the absolute difference, then advance the pointer sitting at the smaller index. That's O(a + b) per query, where a and b are the occurrence counts. The pitfall is worst-case inputs: if a query repeats a frequent word, you re-merge long lists each time. A cache keyed on the word pair fixes repeats cheaply. Another trap is the character cap of 2 * 10^6, so don't build per-query string copies you don't need. Use binary search on the longer list if you want O(min * log max). If the pointer-advance rule slips your mind mid-assessment, StealthCoder is the hedge that reads the problem and hands you the merge logic.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Repeated Shortest Word Distance Queries cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as shortest word distance ii. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass LinkedIn's OA.
LinkedIn reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Repeated Shortest Word Distance Queries FAQ
What's the trick in the LinkedIn repeated shortest word distance question?+
Preprocess once into a map of word to sorted index list. Then each query is a merge of two sorted lists with two pointers. Never rescan the full array per query. That one change takes you from quadratic-scale work to something that passes the limits.
How hard is this OA problem really?+
Medium. The idea is short once you spot it: hash map plus two pointers. Most failures come from the brute-force rescan, not from hard logic. If you've seen the single-query version, this is the same thing with caching of the index lists.
Why advance the pointer at the smaller index?+
The smaller index can only get closer by moving forward, since the other list is sorted. Moving the larger one would only widen the gap. Each step discards an index that can't improve the answer, so the merge finishes in linear time over both lists.
Do I need to worry about repeated queries or heavy words?+
Yes, a little. A word appearing 100000 times in many queries makes repeated merges costly. Cache results by the word pair, and consider binary searching each index of the shorter list in the longer one. It's cheap insurance against adversarial tests.
How do I prepare for this in 48 hours?+
Write the preprocessing map and the two-pointer merge from memory twice. Then test on the adjacent-occurrence case like example 2 and a case where words repeat at the array ends. Keep the function signature in mind: queries in, distances out in the same order.