Shortest Word Distance III
A medium-tier problem at 59% community acceptance, tagged with Array, String. Reported in interviews at Palantir Technologies and 1 others.
Shortest Word Distance III is a medium-difficulty array and string problem that shows up in assessments at Palantir Technologies and LinkedIn. You're given a string array and two words, and you need to return the shortest distance between them anywhere in the array. The trick is that word1 and word2 can be the same, which breaks the naive two-pointer approach. About 59% of candidates solve it, which means if you hit this in a live OA and blank on the word-equality edge case, StealthCoder surfaces a working solution in seconds, invisible to the proctor.
Companies that ask "Shortest Word Distance III"
Shortest Word Distance III 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. Built by an Amazon engineer who realized the OA tests how well you memorized 200 problems, not how well you code.
Get StealthCoderMost candidates treat this like a standard two-pointer problem: track indices of both words and compute distances as you scan left to right. The failure mode happens when word1 equals word2. Suddenly you can't just update two independent pointers and compare their difference. You need to catch the closest pair of different indices that both match the target word. The pattern: as you iterate through the array, whenever you find either word, store the last seen index and check its distance to the previously stored index. When words are identical, you're naturally comparing consecutive occurrences. The gotcha is the third line of logic, not the first two. If you drill only the different-word variant, you'll stumble on the equal-word case in your assessment. StealthCoder is the hedge when that curveball lands.
Pattern tags
You know the problem.
Make sure you actually pass it.
Shortest Word Distance III 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. Built by an Amazon engineer who realized the OA tests how well you memorized 200 problems, not how well you code. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Word Distance III interview FAQ
Is Shortest Word Distance III still asked at FAANG and growth companies?+
Palantir and LinkedIn both report it. At 59% acceptance, it's solidly in the live-round rotation. The equal-words edge case is the stopper, so it filters candidates who code without thinking through all inputs first. It's worth prepping.
What's the actual trick that catches people?+
The assumption that word1 and word2 are always different. When they're identical, your two separate index variables collapse into one concept: find the minimum distance between any two occurrences of the same word. One pointer, updated on every match, then compare to the prior occurrence.
Do I need to handle case sensitivity or whitespace?+
The input is typically a string array of pre-split words, so each element is already a token. Compare strings directly without trimming or case conversion unless the problem explicitly states otherwise. Check the problem statement for this detail.
What's the time and space complexity?+
Single pass through the array gives you O(n) time, where n is the array length. Space is O(1) because you only track a couple of indices and the last seen position, no extra structures. This is the expected solution; anything worse will time out on large inputs.
How does this relate to the original Shortest Word Distance (I)?+
Part I assumes the two words are always different. Part III removes that assumption and adds the equal-words case. If you've solved Part I, you have 80% of the logic. The last 20% is handling the identity case, which is why the problem sits at medium difficulty rather than easy.
Want the actual problem statement? View "Shortest Word Distance III" on LeetCode →