Intervals Between Identical Elements
A medium-tier problem at 45% community acceptance, tagged with Array, Hash Table, Prefix Sum. Reported in interviews at TuSimple and 0 others.
You're given an array and need to find the distance between every pair of identical elements. TuSimple has asked this one, and it trips up candidates who don't think about the problem structure upfront. The naive approach is to compare every pair, but that's O(n²) and fails on large inputs. The trick is realizing you don't need pairwise comparisons at all. Instead, you track positions as you iterate once through the array, computing intervals on the fly. This is the kind of problem where the right framing cuts your code from 20 lines to 5. If you blank during the OA and hit this one, StealthCoder surfaces the working solution in seconds, invisible to the proctor.
Companies that ask "Intervals Between Identical Elements"
Intervals Between Identical Elements 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 StealthCoderThe core pattern: iterate through the array once, store the last seen index of each element in a hash table, and when you see a duplicate, calculate the interval between its current position and last position. No nested loops, no comparing all pairs. Common pitfall is overthinking it as a two-pointer or sorting problem when it's really just hash table bookkeeping. Another trap is trying to precompute all intervals upfront instead of generating them as you go. The prefix sum topic listed here refers to range-query optimization if the problem extends to answering multiple queries about intervals, but the core solution is hash table plus single pass. On a live assessment, if you get stuck on the iteration logic, StealthCoder runs invisibly and hands you the pattern immediately.
Pattern tags
You know the problem.
Make sure you actually pass it.
Intervals Between Identical Elements 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.
Intervals Between Identical Elements interview FAQ
Is this problem actually hard or just unfamiliar?+
It's unfamiliar. The acceptance rate is 44.9%, which is typical for medium problems that punish over-complication. Once you see the hash table approach, it's straightforward. The barrier is recognizing you don't need nested loops, not the math.
How does this relate to the array and hash table topics?+
Array gives you the input structure. Hash table is the actual solution: map each element to its last seen index. When you encounter a duplicate, you have both positions instantly. No traversal, no backtracking.
What's the time complexity and can it be optimized?+
Single pass through the array, O(n) time and O(n) space for the hash table. That's optimal. If you're writing nested loops, you've missed the pattern. Hash table approach is the solution, not an intermediate step.
Why is prefix sum mentioned as a topic if the solution is just a hash table?+
Prefix sum applies if the problem extends to range queries or cumulative interval calculations. For the base problem, hash table plus one pass is the direct path. Prefix sum is secondary or variant-problem territory.
How do I avoid writing O(n²) during the OA?+
Stop and ask yourself: do I actually need to compare every pair? Here, no. You only need each element's most recent position. That's one hash table lookup per element. If you're nesting loops, reframe the problem before coding.
Want the actual problem statement? View "Intervals Between Identical Elements" on LeetCode →