Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given a 0-indexed circular string array words and a string target. A circular array means that the array's end connects to the array's beginning.
words[i] is words[(i + 1) % n] and the previous element of words[i] is words[(i - 1 + n) % n], where n is the length of words.Starting from startIndex, you can move to either the next word or the previous word with 1 step at a time.
Return the shortest distance needed to reach the string target. If the string target does not exist in words, return -1.
Example 1:
Input: words = ["hello","i","am","leetcode","hello"], target = "hello", startIndex = 1 Output: 1 Explanation: We start from index 1 and can reach "hello" by - moving 3 units to the right to reach index 4. - moving 2 units to the left to reach index 4. - moving 4 units to the right to reach index 0. - moving 1 unit to the left to reach index 0. The shortest distance to reach "hello" is 1.
Example 2:
Input: words = ["a","b","leetcode"], target = "leetcode", startIndex = 0 Output: 1 Explanation: We start from index 0 and can reach "leetcode" by - moving 2 units to the right to reach index 3. - moving 1 unit to the left to reach index 3. The shortest distance to reach "leetcode" is 1.
Example 3:
Input: words = ["i","eat","leetcode"], target = "ate", startIndex = 0
Output: -1
Explanation: Since "ate" does not exist in words, we return -1.
Constraints:
1 <= words.length <= 1001 <= words[i].length <= 100words[i] and target consist of only lowercase English letters.0 <= startIndex < words.lengthProblem summary: You are given a 0-indexed circular string array words and a string target. A circular array means that the array's end connects to the array's beginning. Formally, the next element of words[i] is words[(i + 1) % n] and the previous element of words[i] is words[(i - 1 + n) % n], where n is the length of words. Starting from startIndex, you can move to either the next word or the previous word with 1 step at a time. Return the shortest distance needed to reach the string target. If the string target does not exist in words, return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
["hello","i","am","leetcode","hello"] "hello" 1
["a","b","leetcode"] "leetcode" 0
["i","eat","leetcode"] "ate" 0
defuse-the-bomb)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2515: Shortest Distance to Target String in a Circular Array
class Solution {
public int closetTarget(String[] words, String target, int startIndex) {
int n = words.length;
int ans = n;
for (int i = 0; i < n; ++i) {
String w = words[i];
if (w.equals(target)) {
int t = Math.abs(i - startIndex);
ans = Math.min(ans, Math.min(t, n - t));
}
}
return ans == n ? -1 : ans;
}
}
// Accepted solution for LeetCode #2515: Shortest Distance to Target String in a Circular Array
func closetTarget(words []string, target string, startIndex int) int {
n := len(words)
ans := n
for i, w := range words {
if w == target {
t := abs(i - startIndex)
ans = min(ans, min(t, n-t))
}
}
if ans == n {
return -1
}
return ans
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #2515: Shortest Distance to Target String in a Circular Array
class Solution:
def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:
n = len(words)
ans = n
for i, w in enumerate(words):
if w == target:
t = abs(i - startIndex)
ans = min(ans, t, n - t)
return -1 if ans == n else ans
// Accepted solution for LeetCode #2515: Shortest Distance to Target String in a Circular Array
impl Solution {
pub fn closet_target(words: Vec<String>, target: String, start_index: i32) -> i32 {
let start_index = start_index as usize;
let n = words.len();
for i in 0..=n >> 1 {
if words[(start_index - i + n) % n] == target || words[(start_index + i) % n] == target
{
return i as i32;
}
}
-1
}
}
// Accepted solution for LeetCode #2515: Shortest Distance to Target String in a Circular Array
function closetTarget(words: string[], target: string, startIndex: number): number {
const n = words.length;
for (let i = 0; i <= n >> 1; i++) {
if (words[(startIndex - i + n) % n] === target || words[(startIndex + i) % n] === target) {
return i;
}
}
return -1;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.