MEDIUMasked at 2 companies

Shortest Word Distance II

A medium-tier problem at 62% community acceptance, tagged with Array, Hash Table, Two Pointers. Reported in interviews at LinkedIn and 1 others.

Founder's read

Shortest Word Distance II shows up at LinkedIn and Anduril, and it tests whether you can preprocess data smartly. The problem feels like a brute-force search at first, but once you see it, you'll recognize it's really about trading upfront setup for blazing-fast queries. If you hit this live and your initial approach tanks on the second half, StealthCoder surfaces the preprocessing trick in seconds, invisible to the proctor.

Companies asking
2
Difficulty
MEDIUM
Acceptance
62%

Companies that ask "Shortest Word Distance II"

If this hits your live OA

Shortest Word Distance II is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know.

Get StealthCoder
What this means

The trap is thinking query-by-query. Most candidates iterate through the entire word list per query, which works but bleeds time on repeated calls. The real pattern: use a hash table to store indices where each word appears, then use two pointers to walk both word lists in parallel and find the minimum distance. This transforms O(n) per query into O(k + m), where k and m are the occurrence counts. Common miss: not realizing the preprocessing step is the entire point of the design pattern. When you're in the assessment and realize your naive loop won't pass the time limit, StealthCoder gives you the index-list approach without the debug penalty.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Shortest Word Distance II recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Shortest Word Distance II interview FAQ

Why is preprocessing required here?+

The problem implies repeated distance queries on the same word list. Preprocessing indices once means each query runs in linear time relative to occurrences, not the full list. Without it, repeated queries timeout. This is a classic design trade-off: spend upfront compute to speed queries.

Is this actually a hard problem?+

No. The 62% acceptance rate reflects that most people miss the design intent. Once you see that you need a hash map of indices and two-pointer matching, it's straightforward. The 'hard' part is recognizing the pattern during the assessment, not the code itself.

Do I need to handle all strings or just two target words?+

The problem works with any two words from the list. You store indices for all words upfront, then for each shortest(word1, word2) query, you grab the two index lists and iterate in parallel. The two-pointer technique is the key.

How does this relate to the Array and Hash Table topics?+

Hash Table stores word to indices mapping. Array is your index lists and the traversal logic. Two Pointers is how you efficiently find the minimum distance between two sorted index arrays without nested loops.

What's the gotcha on repeated queries?+

If you rebuild indices or re-scan the word list on every query call, you fail the time limit on the second half of test cases. The design pattern locks in preprocessing during initialization so queries are instant. This is why it's a design problem, not just a search problem.

Want the actual problem statement? View "Shortest Word Distance II" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.