K Smallest Squares from a Sorted Array
Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Squares of -1000000000 overflow a 32-bit int, and that's the first trap in this Uber OA question reported in September 2026. You get a sorted array and need the k smallest squares, in order. The statement even hints at the intended path: binary search for the negative/nonnegative split, then merge outward. No full sort. If you've got an invite and 48 hours, this one is learnable tonight. StealthCoder sits invisibly as a safety net during the live OA if your mind goes blank on the merge, but the idea is short enough to own yourself.
The problem
Given an integer array sorted in nondecreasing order, return the k smallest squared values in nondecreasing order. Use 64-bit arithmetic for every square. The intended solution locates the split between negative and nonnegative values with binary search and merges outward from that split without materializing and sorting all squared values. Function kSmallestSquares(nums: int[], k: int) → long[] Examples Example 1 nums = [-7,-3,-1,4,8] k = 3 return = [1,9,16] The three smallest squares come from -1, -3, and 4. Example 2 nums = [-2,-2,0,3] k = 4 return = [0,4,4,9] Duplicate input magnitudes produce duplicate squares. Example 3 nums = [-1000000000,2] k = 1 return = [4] The square of 2 is smaller; 64-bit arithmetic is required for the other value. Constraints 0 <= nums.length <= 200000. 0 <= k <= nums.length. -10^9 <= nums[i] <= 10^9. nums is sorted in nondecreasing order.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: squares grow as you move away from zero. Find the first index with nums[i] >= 0 using binary search. Put one pointer left at i-1 and one right at i. Each step, compare the squares of nums[left] and nums[right], take the smaller, move that pointer, and stop after k picks. That's O(log n + k) time and O(k) output. A heap works too, but it's heavier than needed. Pitfalls: computing squares in int and overflowing, forgetting one pointer runs out of bounds (all negatives or all nonnegatives), and not handling k = 0 or an empty array. Ties are fine, duplicates just appear twice. Cast to long before multiplying, not after. If you blank during the live OA, StealthCoder can surface the two-pointer merge so you only need to type and check edge cases.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill K Smallest Squares from 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Uber's OA.
Uber reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
K Smallest Squares from a Sorted Array FAQ
What's the trick in K Smallest Squares from a Sorted Array?+
Squares are smallest near zero and grow outward in both directions. Binary search for the first nonnegative index, then run two pointers outward from that split, always taking the smaller square. Stop after k picks. You never sort or square the whole array.
Do I need a heap for this Uber question?+
No. A heap is a valid fallback, but the statement points to binary search plus a merge. Two pointers give O(log n + k) and less code. A heap seeded with both neighbors works if you prefer it, but it adds log factors you don't need.
Where do people lose points on this problem?+
Overflow is the big one. Values reach 10^9, so squares reach 10^18. Cast to long before multiplying. Others: pointer bounds when all values are negative or all nonnegative, and returning wrong output for k = 0 or an empty array.
How hard is it really?+
Easy to medium. It's a variation of the classic squares of a sorted array merge, with a k cutoff and a binary search for the split. If you've seen two-pointer merges, you can write it in ten minutes. The edge cases are what take time.
How do I prepare in 48 hours?+
Write this once from scratch with all three examples as tests. Then test an all-negative array, an all-positive array, k = 0, and an empty input. Also do a quick pass on binary search for the first index meeting a condition, since the split depends on it.