MEDIUMasked at 1 company

Closest Equal Element Queries

A medium-tier problem at 31% community acceptance, tagged with Array, Hash Table, Binary Search. Reported in interviews at Salesforce and 0 others.

Founder's read

You're asked to handle multiple range queries on an array, finding the closest pair of equal elements within a given span. Salesforce has asked this. It's a 31% acceptance rate problem, which means most candidates either miss the preprocessing step or TLE on naive solutions. The trap is thinking you can answer each query independently without precomputation. If this hits your live OA and you blank on the data structure choice, StealthCoder surfaces a working solution in seconds, invisible to the proctor.

Companies asking
1
Difficulty
MEDIUM
Acceptance
31%

Companies that ask "Closest Equal Element Queries"

If this hits your live OA

Closest Equal Element Queries 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 engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage.

Get StealthCoder
What this means

The trick is precomputing where each element appears, then for each query using binary search or a hash table to find the closest equal pair inside the left and right bounds. Naive iteration through all pairs in the range fails on large datasets. You need to store indices grouped by value, then for each query, binary search within those index lists to find the tightest pair that fits the constraints. Most candidates build the index correctly but forget the binary search step, hitting TLE. This combines Array, Hash Table, and Binary Search exactly as the tags promise. StealthCoder is your hedge if you get stuck architecting the index or binary search logic during the live assessment.

Pattern tags

The honest play

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

Closest Equal Element Queries 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 engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Closest Equal Element Queries interview FAQ

Is this actually a Salesforce problem or just a LeetCode Medium?+

Salesforce has reported asking it. One company, 31% accept rate, so it's not a frequent hire signal but it does show up. If you're interviewing there or at similar-sized tech companies, it's worth knowing the pattern.

What's the trick that makes this not TLE?+

Precompute a map of value to sorted list of indices. For each query, iterate the values in range, binary search each value's index list for the closest pair that fits [left, right]. Don't iterate all pairs in the range.

How does Binary Search fit this problem?+

After grouping indices by value, you binary search each value's sorted index list to find indices near the query bounds. This prunes the search space and avoids checking every pair in the range, which is where TLE happens.

Can I solve this with just Hash Table, no Binary Search?+

Not efficiently at scale. Hash Table lets you group indices by value, but you still need Binary Search to quickly find the closest indices within the range bounds. Both topics are required for the optimal solution.

What do candidates usually get wrong?+

Building the index correctly but then iterating all pairs in the range instead of binary searching. Or forgetting to track both the distance and the actual indices, losing track of which pair is closest. Test your edge cases around range boundaries.

Want the actual problem statement? View "Closest Equal Element Queries" 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.