Choose K pickup locations minimizing L1 distance
Company: Uber
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates facility-location and spatial optimization skills under the Manhattan (L1) metric, testing algorithm design, combinatorial optimization, and handling geometric data.
Constraints
- 1 <= N (number of people) -- inputs are small enough that brute force over K-subsets is feasible.
- 1 <= K; if K >= N the answer is 0.
- Coordinates are integers (may be negative).
- Pickup locations must be placed at one of the given people's coordinates.
- Duplicate coordinates are allowed.
Examples
Input: ([[0, 0], [0, 1], [10, 10], [10, 11]], 2)
Expected Output: 2
Explanation: Place centers at (0,0) and (10,10). Person (0,1) is 1 away, person (10,11) is 1 away, the two centers cost 0. Total = 2.
Input: ([[0, 0], [2, 0], [4, 0]], 1)
Expected Output: 4
Explanation: With one center, the optimal point among the three is the median (2,0): |0-2| + 0 + |4-2| = 2 + 0 + 2 = 4.
Input: ([[0, 0]], 1)
Expected Output: 0
Explanation: A single person hosts the only pickup location, so the distance is 0.
Input: ([[1, 1], [1, 1], [1, 1]], 1)
Expected Output: 0
Explanation: All people share one coordinate; one center there gives 0 total distance even with K=1 < N.
Input: ([[0, 0], [1, 0], [5, 0], [6, 0]], 2)
Expected Output: 2
Explanation: Two clusters {0,1} and {5,6}. Centers at (1,0) and (5,0): costs 1 + 0 + 0 + 1 = 2.
Input: ([[0, 0], [3, 4]], 2)
Expected Output: 0
Explanation: K equals N, so each person becomes its own pickup location and the total distance is 0.
Input: ([[-2, -2], [-2, 2], [2, -2], [2, 2], [0, 0]], 1)
Expected Output: 16
Explanation: With one center the best choice is the central point (0,0); each of the four corners is L1 distance 4 away (16 total) and (0,0) itself is 0. No corner center does better.
Input: ([[0, 0], [10, 0], [0, 10], [10, 10]], 3)
Expected Output: 10
Explanation: Choose any three of the four corners as centers; the remaining corner's nearest center is an adjacent corner at L1 distance 10. Total = 10.
Hints
- Clarify the rules first: must centers be integer coordinates, and must they be chosen from existing people? Here we assume centers must be one of the given points -- a standard facility-location simplification that makes an exact answer tractable.
- If K >= N, you can give every person their own pickup point, so the total distance is 0. Handle this special case up front.
- For each candidate set of K centers, the cost of a person is the L1 distance to their NEAREST center -- not to all of them. Take the min over centers, then sum over people.
- With centers restricted to the given points and small N, enumerate every K-subset of points (itertools.combinations) and keep the best total. This is exact.
- For larger inputs, discuss heuristics: k-medians local search (Lloyd-style swap), or note that the 1D version decomposes (sort + DP) while the 2D L1 version couples x and y through the assignment.