Allocate Mailboxes
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Bloomberg reported this one in March 2020, and the input size is the tell. Up to 100 houses, k up to 100, positions to 10000. Trying every placement of k mailboxes blows up fast, so the OA wants a partition DP. This is Allocate Mailboxes: sort the houses, split them into k contiguous groups, and put one mailbox at the median of each group. If you've got an invite and you blank on how to cost a group, StealthCoder is the invisible safety net that reads the problem on screen and hands you the solution live. Know the shape before you sit down.
The problem
Houses stand at the integer positions in houses along one street. Install exactly k mailboxes at integer positions so that the sum of every house's distance to its nearest mailbox is as small as possible. Return that minimum total distance. Function minMailboxDistance(houses: int[], k: int) → int Examples Example 1 houses = [5,10,15,20] k = 3 return = 5 Mailboxes at 5, 10, and any median position between 15 and 20 give total distance 5. Example 2 houses = [6,7,8,12] k = 2 return = 2 Positions 7 and 12 serve the houses with total distance 2. Constraints 1 <= houses.length <= 100. 1 <= k <= houses.length. House positions are distinct integers between 1 and 10000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick has two layers. First, for any group of houses, the best single mailbox sits at the median, so the group cost is the sum of distances to the median. Precompute cost[i][j] for every sorted range in O(n^3) or O(n^2) with a running formula. Second, dp[i][m] is the minimum total for the first i houses using m mailboxes. Transition: dp[i][m] = min over j of dp[j][m-1] + cost[j][i-1]. That's O(n^2 * k), tiny for n = 100. The common pitfall is forgetting to sort first, or assuming mailboxes must sit on a house. The median always works, and with distinct integer positions it lands on one anyway. Also watch the base case: dp[0][0] = 0, everything else infinity. If the recurrence slips under pressure, StealthCoder is your hedge during the live OA.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Allocate Mailboxes 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg 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.
Allocate Mailboxes FAQ
What's the trick in Allocate Mailboxes?+
Sort the houses, then split them into k contiguous groups. Each group gets one mailbox at its median, which minimizes the sum of absolute distances. Precompute the cost of every range, then run a DP over how many houses and mailboxes you've used so far.
How hard is this really?+
It's a hard-tagged problem, but the code is short once you see it. The difficulty is spotting the partition DP and the median fact. With n at most 100, you don't need anything clever on performance, just a clean O(n^2 * k) solution.
Why is the median the right mailbox spot?+
For absolute distances, moving the mailbox toward the median always reduces total distance until you pass the middle house. With an even count, any point between the two middle houses ties. Since positions are integers, picking the middle house works fine.
Is greedy enough here?+
No. Greedy placement, like putting mailboxes at the largest gaps, fails on tricky inputs. Example 2 with [6,7,8,12] and k = 2 shows how grouping matters. You need DP to compare every way of splitting the sorted houses into k groups.
How do I prepare in 48 hours for this Bloomberg OA?+
Write the solution from scratch twice. First build the cost table for sorted ranges, then the DP with dp[0][0] = 0. Test on both examples, plus k = 1 and k = n edge cases. If you can explain why groups are contiguous, you're ready.