Reported September 2026
Amazonbinary search

K Closest Elements 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

Amazon reported this one in September 2026, and the input size is the whole point. With arr up to 100000 elements, sorting by distance to x works but wastes the fact that the array is already sorted. The OA wants you to see that the answer is a contiguous window, then find its left edge with binary search. If you blank on that under the clock, StealthCoder is a desktop overlay that stays invisible during the live assessment and can hand you the approach. Know the trick first and you probably won't need it.

The problem

Given an integer array arr sorted in nondecreasing order, an integer k, and a target x, return the k values closest to x in ascending order.
A value a is closer than a value b when |a - x| < |b - x|. When the distances are equal, the smaller value is considered closer.

Function
findClosestElements(arr: int[], k: int, x: int) → int[]

Examples
Example 1
arr = [-10,-4,-1,3,8,12]
k = 4
x = 2
return = [-4,-1,3,8]
Values -4 and 8 are equally distant from 2; the smaller value wins the boundary tie.
Example 2
arr = [2,4,6,8]
k = 1
x = 5
return = [4]
Values 4 and 6 are both one unit away, so the smaller value 4 is selected.
Example 3
arr = [5,6,7]
k = 2
x = 100
return = [6,7]
The target lies to the right of every array value, so the last two values are closest.

Constraints
1 <= arr.length <= 100000.
1 <= k <= arr.length.
arr is sorted in nondecreasing order.
-1000000000 <= arr[i], x <= 1000000000.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: the k closest values always form a contiguous window in a sorted array. So you only need the window's left index, somewhere in 0 to n-k. Binary search on that index. For a candidate left index mid, compare x - arr[mid] with arr[mid+k] - x. If x - arr[mid] is larger, the window should move right, so left = mid+1. Otherwise right = mid. The tie rule favors the smaller value, which is why equal distances keep the left side. Use a strict greater-than check and you get it right. The common pitfall is the two-pointer shrink, which is O(n) and fine here but easy to botch on ties. Another is sorting by distance, which costs O(n log n) and needs a re-sort to ascending. Values reach 1e9, so differences fit in 32-bit ints only barely. Use longs if you're unsure. If the binary search logic slips mid-assessment, StealthCoder is the hedge that gives you the working version live.

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 K Closest Elements 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 find k closest elements. 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.

K Closest Elements in a Sorted Array FAQ

What's the trick to K Closest Elements?+

The answer is always a contiguous window of length k in the sorted array. Binary search the window's left index between 0 and n-k. Compare x - arr[mid] against arr[mid+k] - x to decide whether to slide right. That gives O(log(n-k) + k) time.

How do I handle ties in distance?+

The smaller value wins. In the binary search, move left to mid+1 only when x - arr[mid] is strictly greater than arr[mid+k] - x. On equality, keep the window on the left, which includes the smaller value. Test with arr = [2,4,6,8], k = 1, x = 5 and expect [4].

Is brute force acceptable with 100000 elements?+

Sorting by distance is O(n log n), which likely passes for 100000, but it ignores the sorted input and needs a final sort back to ascending. Amazon-style OAs often have hidden larger tests, so the binary search or two-pointer approach is safer and cleaner.

What edge cases break solutions?+

x far to the right or left of every value, like arr = [5,6,7], k = 2, x = 100, which should return [6,7]. Also k equal to the array length, duplicates in the array, and negative values. Keep the search range at n-k so mid+k never goes out of bounds.

How do I prepare for this in 48 hours?+

Write the binary search version from scratch twice and the two-pointer shrink once. Run all three given examples by hand, including the tie case. Then do a quick pass on lower-bound style binary search, since the same boundary reasoning shows up in many array 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