Place K pickup points to minimize total L1 distance
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Uber's coding round, last asked Dec 2025. It tests clustering, geometry and optimization.
You get the coordinates of N people on a grid and an integer K. Open K shuttle pickup points so that the total Manhattan (L1) distance from each person to their nearest pickup point is as small as possible, and return that minimum total. Settle first where pickup points may go; the answer below assumes they must sit on one of the people's coordinates.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
min_total_distance(points: list[list[int]], k: int) -> int— each pickup point must be placed at one of the given points. A person's cost ismin over pickups (|x - X| + |y - Y|); return the sum of the costs. Ifk >= N, return0.
Example
min_total_distance([[0, 0], [0, 1], [10, 10], [10, 11]], 2) -> 2 # pickups at (0, 0) and (10, 10)
min_total_distance([[0, 0], [2, 0], [4, 0]], 1) -> 4 # pickup at the median (2, 0)
min_total_distance([[0, 0], [3, 4]], 2) -> 0
Constraints
-
1 <= K. For an exact 2D answer, N is small (a few dozen); the 1D and heuristic versions should handle a few thousand. - Coordinates are integers and may be negative; points may repeat.
Clarifying questions
- Must pickups be at integer coordinates? At people's locations, or anywhere?
- How large are N and K? Is an exact answer required or is a good approximation fine?
- Can several people share a location?
- Should I return the pickup locations as well as the total?
What a strong answer covers
- Recognizing K-medians under L1, which is NP-hard in general, so an exact answer needs small inputs or extra structure.
- K = 1: L1 separates by axis, so the best point anywhere is the median x with the median y; restricted to people, score every point as the pickup, O(N log N) with sorted coordinates and prefix sums per axis.
- Exact search for small inputs: try every K-subset of points as pickups, O(C(N, K) · N · K).
- A 1D version (everyone on a line) solved exactly with sorting and a DP over contiguous groups, each group served by its median.
- A heuristic for larger inputs: assign each person to the nearest pickup, move each pickup to its group's coordinate-wise median, repeat; plus swap-based local search and several random starts.
- Tests: K = 1, K >= N, duplicate points, all people on one line.
Follow-ups
- Pickups may go anywhere on the grid. How does that change the K = 1 answer and the search?
- Each pickup can serve at most C people.
- People arrive over the day. How would you update the pickups without recomputing from scratch?
Where it's asked
- Uber · Coding · Last asked Dec 202560 min · Two rounds; one is often object-oriented design (a parking lot, a meeting scheduler, an expression evaluator) · No AI, except a newer AI-assisted debugging round in some senior loops
What it tests
clustering · geometry · optimization
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
