Reported July 2026
Amazonbinary search

Single Element in a Sorted Array

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

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

The whole Amazon question from July 2026 hinges on one plain sorted array and what its index parity tells you. Single Element in a Sorted Array looks like a hash-map freebie, but the O(log n) time and O(1) space requirement kills that. If you've got the OA in a day or two, this is a binary search with a twist, not a lookup trick. Candidates reported it for Amazon in July 2026. Know the pairing rule and it takes ten minutes. Miss it and you'll burn half your time on XOR and counting ideas that run in O(n). StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment.

The problem

Given a sorted integer array nums, every value appears exactly twice except for one value that appears exactly once.
Return the value that appears once. Your solution must run in O(log n) time and use O(1) extra space.

Function
singleNonDuplicate(nums: int[]) → int

Examples
Example 1
nums = [1,1,2,3,3,4,4]
return = 2
Every value except 2 belongs to an adjacent pair.
Example 2
nums = [0]
return = 0
The only element is the unique value.

Constraints
1 <= nums.length <= 100000
nums.length is odd.
-2147483648 <= nums[i] <= 2147483647
nums is sorted in nondecreasing order.
Exactly one value appears once; every other value appears exactly twice.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is index parity. Before the single element, every pair starts at an even index, so nums[i] == nums[i+1] when i is even. After the single element, pairs start at odd indices, so that equality breaks. Binary search on that. Take mid, and if mid is odd, subtract one so it's even. If nums[mid] == nums[mid+1], the single element is to the right, so set lo = mid + 2. Otherwise it's at mid or to the left, so set hi = mid. Loop while lo < hi and return nums[lo]. The common pitfall is the off-by-one on mid adjustment, or setting hi = mid - 1 and skipping the answer. Another is reaching for XOR of all elements. It's correct but O(n), so it fails the stated bound. If you blank on the parity logic during the live OA, StealthCoder can surface the working solution so you can keep moving.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Single Element in a Sorted Array 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as single element in a sorted array. If you have time before the OA, drill that.

⏵ The honest play

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

Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Single Element in a Sorted Array FAQ

What's the trick for Single Element in a Sorted Array?+

Use index parity. Left of the single element, pairs start on even indices. Right of it, they start on odd indices. Binary search on mid, forced to even, and check whether nums[mid] equals nums[mid+1]. If yes, go right. If not, go left, keeping mid.

Why can't I just XOR everything?+

XOR gives the right answer, since pairs cancel out. But it scans the whole array, so it's O(n). The problem demands O(log n) time, so an OA with hidden performance tests on 100000 elements can reject it. Use binary search instead.

How hard is this really for an Amazon OA?+

Medium difficulty. The idea is short, but the boundary handling trips people. Once you know the even-index pairing rule, the code is about ten lines. The hard part is not recognizing that a sorted array with pairs invites binary search.

What edge cases should I test?+

Test a single-element array like [0], where the answer is that element. Test the single value at the very start and the very end of the array. Also test negative numbers and the largest length. Your mid+1 access must never go out of bounds, so use lo < hi.

How do I prepare for this in 48 hours?+

Write the parity binary search from scratch twice, without looking. Then trace [1,1,2,3,3,4,4] by hand and check each lo, hi, and mid value. Also drill the lo < hi with hi = mid template, since that's the pattern that shows up in many similar search problems.

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

OA at Amazon?
Invisible during screen share
Get it