Code Room
CodingHardcod-g935
Subject Interval dpLevel Senior–Staff~40 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.