EASYasked at 3 companies

Shortest Word Distance

A easy-tier problem at 66% community acceptance, tagged with Array, String. Reported in interviews at Wix and 2 others.

Founder's read

Shortest Word Distance is an easy array and string problem with a 66% acceptance rate, yet candidates blank on it regularly because the optimal solution isn't what you'd code first. Wix, Anduril, and LinkedIn have asked it. The problem: given an array of strings and two distinct words, return the minimum distance between any two indices where those words appear. Most people loop and track indices manually, which works but is verbose. The real trick is a single-pass sweep that maintains the most recent position of each word, letting you compute the distance on the fly. If this problem hits your live assessment and you freeze on the logic, StealthCoder surfaces a working solution in seconds, invisible to the proctor.

Companies asking
3
Difficulty
EASY
Acceptance
66%

Companies that ask "Shortest Word Distance"

If this hits your live OA

Shortest Word Distance 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 for the engineer who has done the work but might still blank with a webcam pointed at him.

Get StealthCoder
What this means

The naive approach is a double nested loop comparing every occurrence of word1 against every occurrence of word2, which is O(n^2) and slow. The optimal path is linear and elegant: hold two variables for the last seen index of each word, iterate once through the array, and whenever you see either word, update its index and compute the absolute distance between the two indices. That gives you O(n) time and O(1) space. The trap most people fall into is overcomplicating it with hash maps or extra passes. This problem tests whether you can spot that one-pass stateful sweep is enough. It's not tricky algorithmically, but it's the kind of thing you rush and write a clunky version of under pressure. StealthCoder is your hedge for the live OA if you blank on whether you need to preprocess or if you second-guess yourself into nested loops.

Pattern tags

The honest play

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

Shortest Word Distance recycles across companies for a reason. It's easy-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 for the engineer who has done the work but might still blank with a webcam pointed at him. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Shortest Word Distance interview FAQ

Why is this marked easy but I'm struggling with it?+

Because the trick isn't a hard algorithm, it's recognizing you only need one pass with two pointers. You're probably overthinking it with extra loops or data structures. The problem tests pattern recognition, not advanced coding. Once you see the solution, it's obvious.

Is this still asked at places like LinkedIn and Wix?+

Yes. Wix, Anduril, and LinkedIn report asking it. It's a screening problem at that tier. Not every company uses it, but it's common enough that if it hits your OA, you want to nail it in under 5 minutes.

What if the two words appear multiple times in the array?+

That's the whole problem. You track the last position you saw each word. Every time you encounter either word, you update its position and calculate the distance. The minimum distance across all updates is your answer. One pass handles all occurrences.

Do I need a hash map or dictionary to solve this?+

No. You only need two integer variables to store the last index of each word. A hash map would work but it's overkill and slower. The optimal solution is O(n) time and O(1) space with just two pointers.

What's the most common mistake people make?+

Writing nested loops to compare every pair of indices, which passes but is O(n^2). Or trying to preprocess and store all indices upfront. Both work but are verbose and slow. Candidates also sometimes forget to update the minimum as they iterate, which breaks the answer entirely.

Want the actual problem statement? View "Shortest Word Distance" 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.