Code RoomMailbox assignment distance
HardPrep Room Coding #1498

Mailbox assignment distance

CodingAlgorithms & data structuresSenior–Staff~40 min

Given the positions of houses on a street (integers) and a number k of mailboxes to install (each mailbox sits at an integer position), assign each house to its nearest mailbox. Return the minimum possible total distance between all houses and their nearest mailbox. There are at most 100 houses and 1 <= k <= number of houses; positions are 1..10000.

Implement
min_mailbox_distance(houses: list[int], k: int) → int
Examples
in[[1,4,8,10,20],3]out5
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 40 min
InputExpectedGot
[[1,4,8,10,20],3]5not run yetsample