Find K Closest Elements in a Sorted Array
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole problem hinges on one fact: the array is already sorted, so the answer is a contiguous window. Bloomberg reported this one in July 2020, and it's the classic find-k-closest setup with a tie-break on the smaller value. If you've got an OA coming, you need to spot that the output is a slice, not a pile of scattered picks. Binary search finds where the slice starts. Candidates who reach for sorting by distance get a working answer and waste time. If you blank on the window idea during the live assessment, StealthCoder runs invisibly on your desktop and gives you the approach as a safety net.
The problem
Given a sorted integer array arr, an integer k, and a target x, return the k closest values to x in ascending order. A value a is closer than b when |a - x| < |b - x|. For equal distances, the smaller value is closer. Function findClosestElements(arr: int[], k: int, x: int) → int[] Examples Example 1 arr = [1,2,3,4,5] k = 4 x = 3 return = [1,2,3,4] The four smallest distances belong to 1, 2, 3, and 4. Example 2 arr = [1,2,3,4,5] k = 4 x = -1 return = [1,2,3,4] The target lies left of the array, so the first four values are closest. Constraints arr is non-empty and sorted in nondecreasing order. 1 <= k <= arr.length. All values and x are signed integers.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: the k closest elements always form a contiguous block of length k. So binary search the left edge of that block over the range 0 to n-k. At mid, compare the two candidates at the boundary: x - arr[mid] versus arr[mid+k] - x. If x - arr[mid] is greater, the window should shift right, so set lo = mid+1. Otherwise set hi = mid. Return arr[lo:lo+k]. That's O(log(n-k) + k). The common pitfall is the tie-break. On equal distance the smaller value wins, so use a strict greater-than when shifting right. Another trap is searching up to n instead of n-k, which causes an out-of-bounds read on arr[mid+k]. Sorting by distance with a custom comparator also works but costs O(n log n) and ignores the sorted input. Keep the final slice in ascending order. StealthCoder is your hedge if the boundary comparison slips your mind mid-assessment.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Find 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as find k closest elements. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find K Closest Elements in a Sorted Array FAQ
How hard is Find K Closest Elements really?+
Medium. The two-pointer shrink approach is easy to write. The binary search on the window start is the version that trips people, mostly on the boundary comparison and the search range. Once you see the answer is a contiguous slice, the code is about ten lines.
What's the trick to solving it fast?+
Binary search the window's left edge, not the value x. Search lo=0, hi=n-k. Compare x - arr[mid] against arr[mid+k] - x. If the left side is farther, move lo to mid+1. Otherwise hi becomes mid. Return the slice starting at lo with length k.
How do I handle ties in distance?+
The smaller value wins on a tie. In the binary search, that means you only shift the window right when the left element is strictly farther than the right one. Using greater-than-or-equal there flips tie behavior and gives wrong answers on cases like equal distances on both sides.
Can I just sort by distance instead?+
Yes, sort by absolute distance with the value as a tiebreaker, take the first k, then sort them ascending. It's O(n log n) and correct. It ignores the sorted input though, so expect to be asked for the faster approach if the assessment grades on efficiency.
How do I prep for this in 48 hours?+
Write the binary search window version from scratch twice. Test it on x below the array, x above it, and x with tied distances. Those three cases cover nearly every bug. Then skim the two-pointer shrink version as a fallback you can code quickly.