Reported December 2021
ZipRecruiterarray

Find the Smallest Magic Index

Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live ZipRecruiter OA. Under 2s to a working solution.
Founder's read

ZipRecruiter reported this one in December 2021, and it looks too easy to be worth a worry. Find the smallest index where values[i] equals i, or return -1. The input goes up to 100000 elements, so a plain scan is fine, but the examples hint at something more. Neither example says the array is sorted, and the statement never promises it. That detail decides your approach. If you blank on it during the OA, StealthCoder sits invisibly on your screen as a safety net and reads the problem for you. Know the trick before you open the invite.

The problem

An index i is magic when values[i] == i using zero-based indexing.
Return the smallest magic index, or -1 when no such index exists.

Function
smallestMagicIndex(values: int[]) → int

Examples
Example 1
values = [-1,0,2,5]
return = 2
Index two is the first position equal to its value.
Example 2
values = [1,2,3]
return = -1
No position equals its zero-based index.

Constraints
0 <= values.length <= 100000
-1000000000 <= values[i] <= 1000000000

Reported by candidates. Source: FastPrep

Pattern and pitfall

The statement never says the array is sorted or distinct. Example 1 happens to be sorted, but that's not a guarantee. If you assume sorted and binary search, you can return a wrong answer on unsorted input. The safe solution is a single linear pass from index 0 upward, returning the first i where values[i] == i. That's O(n) time and O(1) space, and with n at 100000 it's plenty fast. Returning on the first match gives you the smallest index for free. Pitfalls: handling an empty array (return -1), and not mixing up value and index. If the real prompt says sorted and distinct, binary search works in O(log n), since values[i] - i is non-decreasing. With duplicates it breaks. If you freeze on the assumptions during the live OA, StealthCoder can surface the linear solution while you confirm the constraints.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Find the Smallest Magic Index cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass ZipRecruiter's OA.

ZipRecruiter reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Find the Smallest Magic Index FAQ

How hard is Find the Smallest Magic Index really?+

It's easy if you just scan. The array length caps at 100000, so O(n) passes comfortably. The only difficulty is overthinking it. Don't reach for a clever algorithm until the statement actually guarantees sorted order.

What's the trick to this problem?+

Loop through the array and return the first i where values[i] == i. Returning early guarantees the smallest index. Return -1 after the loop. The trick is not assuming sortedness, because the statement never promises it.

Can I use binary search here?+

Only if the array is guaranteed sorted with distinct integers. Then values[i] - i is non-decreasing, so you can binary search for the leftmost zero. Without that guarantee, it can skip valid indices. Linear is the safe default.

What edge cases should I test?+

Test an empty array, which returns -1. Test a match at index 0, a match only at the last index, negative values, and multiple matches where you must return the smallest. Also test values far outside the index range, up to a billion in size.

How do I prepare for this in 48 hours?+

Write the linear scan from memory, then write the sorted-distinct binary search variant and know why it works. Run both on the two examples and on an empty array. That covers every version this problem is likely to take in the OA.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with ZipRecruiter.

OA at ZipRecruiter?
Invisible during screen share
Get it