Reported October 2022
ZipRecruitersimulation

Distribute Values by Distance from Current Averages

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

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

The ZipRecruiter OA reported in October 2022 hands you two seeds, values[0] and values[1], then asks you to sort every later number by how close it sits to each array's running mean. It's a simulation problem dressed up as math. The statement is short, the tie rule is the only gotcha, and the input can hit 100000 elements, so sloppy work gets punished. If you've got an invite and 48 hours, learn the shape of this one now. StealthCoder sits invisibly on your screen as a safety net if you blank during the live OA, but the logic below is simple enough to own.

The problem

Initialize first with values[0] and second with values[1]. For every later value, compare its absolute distance from each array's current arithmetic mean and append it to the array with the smaller distance. Break an exact tie in favor of first.
Return first followed by second.

Function
distributeByAverage(values: int[]) → int[]

Examples
Example 1
values = [1,10,2,9,5]
return = [1,2,5,10,9]
Later values are compared against evolving means.
Example 2
values = [0,10,5]
return = [0,5,10]
Equal average distances choose the first array.

Constraints
2 <= values.length <= 100000
-100000 <= values[i] <= 100000

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: don't recompute the average from scratch. Keep a running sum and a count for each array. For each new value v, compute mean1 = sum1/count1 and mean2 = sum2/count2, then compare abs(v - mean1) against abs(v - mean2). Smaller distance wins, and an exact tie goes to first. That's O(n) total. The pitfall is floating point. Division can create false ties or false differences, so avoid it. Compare abs(v*c1 - s1) * c2 against abs(v*c2 - s2) * c1 using integers. Sums reach about 1e10 and products about 1e15, so use 64-bit types. Example 2 checks the tie rule: after [0] and [10], the value 5 is 5 from both means, so it lands in first. If you freeze on the integer comparison during the live OA, StealthCoder can hand you the cross-multiplied version while you keep typing.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Distribute Values by Distance from Current Averages 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

ZipRecruiter reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Distribute Values by Distance from Current Averages FAQ

How hard is the ZipRecruiter distribute-by-average problem really?+

It's easy to medium. The algorithm is a single pass with running sums. The difficulty is precision and the tie rule, not the idea. If you can maintain two counters and compare distances correctly, you're done in under twenty lines.

What's the trick to avoid floating point errors?+

Skip division. Compare abs(v*c1 - s1) * c2 with abs(v*c2 - s2) * c1, where s is the sum and c is the count. Both means share the same form, so cross-multiplying keeps everything in integers. Use 64-bit types because products can reach roughly 1e15.

How should I handle ties?+

Use a strict less-than for the second array. Append to second only if its distance is strictly smaller than first's. Any equal distance then falls through to first, which matches the rule and Example 2 where 5 is equally far from 0 and 10.

Does the order of the output matter?+

Yes. Return first followed by second, and keep each array in the order values were appended. Example 1 gives [1,2,5,10,9], so don't sort anything. Just concatenate the two lists at the end.

How do I prepare for this in 48 hours?+

Write it once from scratch with running sums and the integer comparison. Test Example 1 and Example 2 by hand, then try negatives and a large input of 100000 values. That covers every real risk, so a couple of hours is enough.

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

OA at ZipRecruiter?
Invisible during screen share
Get it