The Find Occurrences of an Element in an Array interview question is a query-based task. You are given an array nums, a target value x, and a set of queries. Each query asks for the index of the occurrence of x in the array. If x appears fewer than times, you should return -1 for that query. This Find Occurrences coding problem is about pre-processing data to handle multiple lookups efficiently.
Companies like J.P. Morgan and IBM use this to test your ability to move beyond searches for every query. It evaluations whether you can identify that a single pass over the array can store all necessary information in a Hash Table interview pattern or a simple list, turning subsequent queries into operations.
This problem follows the Pre-processing with a List/Map pattern.
arr[i] == x.posList).posList, return -1.posList[q - 1] (using 0-based indexing for the list).Array: [1, 3, 1, 7, 1, 2], target , Queries: [1, 3, 4]
[0, 2, 4].[0, 4, -1].Always ask the interviewer about the number of queries. If there are many queries, pre-processing the data into an lookup structure is almost always the expected Array interview pattern solution.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum Linear Stock Score | Medium | Solve | |
| 4Sum II | Medium | Solve | |
| Finding the Users Active Minutes | Medium | Solve | |
| Find the Number of Good Pairs II | Medium | Solve | |
| Convert an Array Into a 2D Array With Conditions | Medium | Solve |