Reported October 2024
Amazonsorting

Find Min Variance

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

You've got an Amazon OA coming in the next day or two with a min variance problem, and you're probably wondering if this is a trick question or a slog. It's neither. The challenge is to split or rearrange a dataset so the spread between max and min is as small as possible. Candidates often overthink the sorting step or miss that you're optimizing for a specific metric, not minimizing raw distance. StealthCoder sits in the background during your OA to catch you if the pattern doesn't click in the moment.

Pattern and pitfall

Min variance problems almost always hinge on sorting first. Once sorted, you're either partitioning the array into contiguous chunks and finding the chunk with the smallest range, or you're selecting elements that minimize variance directly. The trick: sorted arrays make range-finding trivial because max and min of any subarray are its endpoints. Most candidates waste time on unsorted comparisons or try greedy selection without establishing order. The math is clean once you sort, but the insight requires seeing that variance or range is monotonic across sorted subarrays. StealthCoder can surface the right approach instantly if you're stuck on whether to sort, partition, or iterate.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Find Min Variance 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 StealthCoder

Related leaked OAs

⏵ The honest play

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

Amazon 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 Min Variance FAQ

Is this a sorting problem or a greedy problem?+

Both. You sort first to establish structure, then greedily select the window or partition with minimum range. Sorting is the precondition; greedy is the search strategy.

What's the most common mistake?+

Trying to minimize variance without sorting, or forgetting that in a sorted array, the range of any subarray is just last minus first. You'll waste time comparing unsorted pairs.

Do I need to generate all partitions?+

No. If you're splitting into k groups, iterate through sorted windows. If you're selecting m elements, use a sliding window of size m and track the min range.

What if the problem asks for the actual partition, not just the min variance?+

Track the start and end indices of the optimal window as you iterate. Store them and reconstruct the partition at the end.

How do I code this in 15 minutes?+

Sort, loop through all contiguous subarrays (or sliding windows of a fixed size), calculate range for each, track the minimum. Three nested lines of logic once you sort.

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