Reported July 2020
Bloombergbinary search

Sum of Mutated Array Closest to Target

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

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

The Bloomberg OA reported in July 2020 hands you a mutated-array sum and asks for the best cap. Strip the story and it's one question: which cap makes sum(min(x, cap)) land nearest the target? That sum only goes up as the cap goes up, so you can search for the answer instead of trying every cap. If you freeze on the tie-break or the search bounds during the assessment, StealthCoder runs invisibly on your desktop and gives you a working solution as a safety net. Know the shape first, though. It's short once you see it.

The problem

Choose a nonnegative integer cap. Replace every nums[i] > cap by cap and leave smaller values unchanged. Return the cap whose mutated-array sum is closest to target; break ties by smaller cap.

Function
findBestValue(nums: int[], target: int) → int

Examples
Example 1
nums = [4,9,3]
target = 10
return = 3
Cap 3 gives sum 9, closer than cap 4's sum 11; the distances tie, so choose 3.

Constraints
1 <= nums.length <= 10^5.
Values and target are positive integers.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is monotonicity. As the cap rises, the mutated sum never drops. So binary search the cap from 0 to max(nums), find the smallest cap whose sum is at least target, then compare that cap against cap-1 and pick whichever gives the smaller distance. On a tie, take the smaller cap. The alternative is sort plus prefix sums. Walk the sorted array, and at each index compute the cap that would spread the remaining target evenly across the remaining elements, then round it. Common pitfalls: skipping the cap-1 check, flipping the tie-break, and recomputing the sum badly so you drift past O(n log m). With n up to 10^5, an O(n * max) brute force over every cap is risky. Watch the edge case where the target exceeds the total sum. The answer is then max(nums). If you blank mid-assessment, StealthCoder is the hedge that reads the problem and hands you the binary search.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Sum of Mutated Array Closest to Target 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Bloomberg reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Sum of Mutated Array Closest to Target FAQ

What's the trick in Sum of Mutated Array Closest to Target?+

The mutated sum is monotonic in the cap. Bigger cap, bigger or equal sum. That lets you binary search the cap instead of testing each one. After finding the boundary, compare the two neighboring caps and pick the closer one, with ties going to the smaller cap.

How hard is this really for the Bloomberg OA?+

Medium. The idea is simple once you spot monotonicity, but the tie-break and boundary checks trip people up. Most failed attempts come from off-by-one errors at the answer, not from the search itself. Test your solution on the [4,9,3], target 10 example.

Can I skip binary search and use sorting?+

Yes. Sort nums, build running prefix sums, and for each position compute the cap that fills the remaining target across the remaining elements. Round it, then check the neighbors. It's the same complexity, dominated by the sort, but it takes more careful arithmetic than binary search.

What edge cases should I test?+

Test a target larger than the total sum, where the answer is max(nums). Test a target smaller than n, where the cap can be 0 or 1. Test the tie case from the example, where caps 3 and 4 are equally close and 3 must win. Test a single-element array.

How do I prepare for this in 48 hours?+

Write the binary search version once from scratch. Define a helper that computes the mutated sum for a cap, search the range 0 to max, then compare the cap and cap-1. Run it on the sample and on your edge cases. That's enough for this pattern.

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

OA at Bloomberg?
Invisible during screen share
Get it