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.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a circular array nums and an array queries.
For each query i, you have to find the following:
queries[i] and any other index j in the circular array, where nums[j] == nums[queries[i]]. If no such index exists, the answer for that query should be -1.Return an array answer of the same size as queries, where answer[i] represents the result for query i.
Example 1:
Input: nums = [1,3,1,4,1,3,2], queries = [0,3,5]
Output: [2,-1,3]
Explanation:
queries[0] = 0 is nums[0] = 1. The nearest index with the same value is 2, and the distance between them is 2.queries[1] = 3 is nums[3] = 4. No other index contains 4, so the result is -1.queries[2] = 5 is nums[5] = 3. The nearest index with the same value is 1, and the distance between them is 3 (following the circular path: 5 -> 6 -> 0 -> 1).Example 2:
Input: nums = [1,2,3,4], queries = [0,1,2,3]
Output: [-1,-1,-1,-1]
Explanation:
Each value in nums is unique, so no index shares the same value as the queried element. This results in -1 for all queries.
Constraints:
1 <= queries.length <= nums.length <= 1051 <= nums[i] <= 1060 <= queries[i] < nums.lengthProblem summary: You are given a circular array nums and an array queries. For each query i, you have to find the following: The minimum distance between the element at index queries[i] and any other index j in the circular array, where nums[j] == nums[queries[i]]. If no such index exists, the answer for that query should be -1. Return an array answer of the same size as queries, where answer[i] represents the result for query i.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Binary Search
[1,3,1,4,1,3,2] [0,3,5]
[1,2,3,4] [0,1,2,3]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3488: Closest Equal Element Queries
class Solution {
public List<Integer> solveQueries(int[] nums, int[] queries) {
int n = nums.length;
int m = n * 2;
int[] d = new int[m];
Arrays.fill(d, m);
Map<Integer, Integer> left = new HashMap<>();
for (int i = 0; i < m; i++) {
int x = nums[i % n];
if (left.containsKey(x)) {
d[i] = Math.min(d[i], i - left.get(x));
}
left.put(x, i);
}
Map<Integer, Integer> right = new HashMap<>();
for (int i = m - 1; i >= 0; i--) {
int x = nums[i % n];
if (right.containsKey(x)) {
d[i] = Math.min(d[i], right.get(x) - i);
}
right.put(x, i);
}
for (int i = 0; i < n; i++) {
d[i] = Math.min(d[i], d[i + n]);
}
List<Integer> ans = new ArrayList<>();
for (int query : queries) {
ans.add(d[query] >= n ? -1 : d[query]);
}
return ans;
}
}
// Accepted solution for LeetCode #3488: Closest Equal Element Queries
func solveQueries(nums []int, queries []int) []int {
n := len(nums)
m := n * 2
d := make([]int, m)
for i := range d {
d[i] = m
}
left := make(map[int]int)
for i := 0; i < m; i++ {
x := nums[i%n]
if idx, exists := left[x]; exists {
d[i] = min(d[i], i-idx)
}
left[x] = i
}
right := make(map[int]int)
for i := m - 1; i >= 0; i-- {
x := nums[i%n]
if idx, exists := right[x]; exists {
d[i] = min(d[i], idx-i)
}
right[x] = i
}
for i := 0; i < n; i++ {
d[i] = min(d[i], d[i+n])
}
ans := make([]int, len(queries))
for i, query := range queries {
if d[query] >= n {
ans[i] = -1
} else {
ans[i] = d[query]
}
}
return ans
}
# Accepted solution for LeetCode #3488: Closest Equal Element Queries
class Solution:
def solveQueries(self, nums: List[int], queries: List[int]) -> List[int]:
n = len(nums)
m = n << 1
d = [m] * m
left = {}
for i in range(m):
x = nums[i % n]
if x in left:
d[i] = min(d[i], i - left[x])
left[x] = i
right = {}
for i in range(m - 1, -1, -1):
x = nums[i % n]
if x in right:
d[i] = min(d[i], right[x] - i)
right[x] = i
for i in range(n):
d[i] = min(d[i], d[i + n])
return [-1 if d[i] >= n else d[i] for i in queries]
// Accepted solution for LeetCode #3488: Closest Equal Element Queries
use std::collections::HashMap;
impl Solution {
pub fn solve_queries(nums: Vec<i32>, queries: Vec<i32>) -> Vec<i32> {
let n = nums.len();
let m = n * 2;
let mut d = vec![m as i32; m];
let mut left = HashMap::new();
for i in 0..m {
let x = nums[i % n];
if let Some(&l) = left.get(&x) {
d[i] = d[i].min((i - l) as i32);
}
left.insert(x, i);
}
let mut right = HashMap::new();
for i in (0..m).rev() {
let x = nums[i % n];
if let Some(&r) = right.get(&x) {
d[i] = d[i].min((r - i) as i32);
}
right.insert(x, i);
}
for i in 0..n {
d[i] = d[i].min(d[i + n]);
}
queries
.iter()
.map(|&query| {
if d[query as usize] >= n as i32 {
-1
} else {
d[query as usize]
}
})
.collect()
}
}
// Accepted solution for LeetCode #3488: Closest Equal Element Queries
function solveQueries(nums: number[], queries: number[]): number[] {
const n = nums.length;
const m = n * 2;
const d: number[] = Array(m).fill(m);
const left = new Map<number, number>();
for (let i = 0; i < m; i++) {
const x = nums[i % n];
if (left.has(x)) {
d[i] = Math.min(d[i], i - left.get(x)!);
}
left.set(x, i);
}
const right = new Map<number, number>();
for (let i = m - 1; i >= 0; i--) {
const x = nums[i % n];
if (right.has(x)) {
d[i] = Math.min(d[i], right.get(x)! - i);
}
right.set(x, i);
}
for (let i = 0; i < n; i++) {
d[i] = Math.min(d[i], d[i + n]);
}
return queries.map(query => (d[query] >= n ? -1 : d[query]));
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
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.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.