MEDIUMasked at 2 companies

Find Occurrences of an Element in an Array

A medium-tier problem at 73% community acceptance, tagged with Array, Hash Table. Reported in interviews at IBM and 1 others.

Founder's read

You're asked to find all occurrences of a target element in an array. Sounds trivial until you realize the real constraint: the array is supposedly sorted, or the element appears with a weird distribution, or you need to do it in a single pass with no hash table. This problem hits the board at IBM and J.P. Morgan, and 72% of candidates pass it, which means it's a filter question. The catch is that naive solutions work but aren't optimal for large datasets. If you blank on the pattern during your live OA, StealthCoder runs invisibly and surfaces the working approach in seconds.

Companies asking
2
Difficulty
MEDIUM
Acceptance
73%

Companies that ask "Find Occurrences of an Element in an Array"

If this hits your live OA

Find Occurrences of an Element in an Array 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 a senior engineer who knows the OA is theater. This is the script.

Get StealthCoder
What this means

The straightforward path is to iterate through the array and collect indices where the value matches your target. Most candidates do this and get accepted. The trap is thinking you need a hash table when the problem screams Array operations. If the array is sorted, binary search cuts the work down dramatically: find the leftmost and rightmost boundaries of your target in log time, then return the range. Unsorted arrays force a linear scan. Common pitfall: candidates overthink it and build a hash map when a single loop suffices. Another mistake is returning indices instead of elements, or vice versa. Read the problem spec carefully. StealthCoder handles both sorted and unsorted variants, so even if you misread the constraint during the assessment, you have a fallback.

Pattern tags

The honest play

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

Find Occurrences of an Element in an Array 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 a senior engineer who knows the OA is theater. This is the script. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Find Occurrences of an Element in an Array interview FAQ

Is this really just a linear scan problem?+

Not always. If the array is sorted, binary search dominates. Unsorted arrays require O(n) time no matter what. The 72% acceptance rate suggests most people get a working solution, but optimized approaches using binary search separate strong candidates from the pack.

Do I need a hash table for this?+

No. Hash tables are overkill here. Array-based techniques (linear scan for unsorted, binary search for sorted) solve it without extra space. Hash tables waste memory and time. The problem tests whether you know when to reach for the right tool.

How does this relate to array vs hash table topics?+

The problem lists both because candidates can solve it either way. Array methods are preferred. Hash Table appears as a red herring or for candidates who don't recognize the Array pattern. Interviewers reward clean Array solutions.

What's the trick if the array is sorted?+

Use binary search to find the left boundary (first occurrence) and right boundary (last occurrence) of the target. This cuts time from O(n) to O(log n). Most candidates miss this optimization and brute force instead.

Why do IBM and J.P. Morgan ask this?+

It filters candidates who understand basic data structure tradeoffs. Unsorted arrays, sorted arrays, hash tables versus arrays, boundary conditions all live in this one problem. It's short but reveals a lot about how someone thinks.

Want the actual problem statement? View "Find Occurrences of an Element in an Array" 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.