Minimum sum of Euclidean distances from one service center to all customers
Quick Overview
Given customer positions on a 2D map, compute the smallest possible sum of Euclidean distances from a single service center to every customer. It tests minimizing a convex function with an iterative method, choosing a numerical stopping rule, and handling a center that lands exactly on a customer.
Minimum sum of Euclidean distances from one service center to all customers
Company: Citadel
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Other
A delivery company wants to build a new service center in a new city. The company knows the positions of all the customers in this city on a 2D map and wants to build the new center in a position such that the sum of the Euclidean distances to all customers is minimum.
Given an array `positions` where `positions[i] = [x_i, y_i]` is the position of the `i`-th customer on the map, return the minimum sum of the Euclidean distances to all customers.
In other words, choose the position of the service center `[x_centre, y_centre]` so that the sum of Euclidean distances from the center to every customer is minimized. This is known as the geometric median problem.
### Function Signature
```python
def min_distance_sum(positions: list[list[int]]) -> float:
```
### Rules
- The center can be placed at any real point of the plane, including a customer's position.
- Several customers may share a position. Each one counts separately in the sum.
- Return the minimum sum as a float. Answers within ${10^{-5}}$ of the true minimum are accepted.
### Constraints
- `1 <= len(positions) <= 50`
- `len(positions[i]) == 2`
- `0 <= x_i, y_i <= 100`
### Examples
```text
Input: positions = [[0, 1], [1, 0], [1, 2], [2, 1]]
Output: 4.0
```
The center at `(1, 1)` is at distance 1 from each customer. No point does better: by the triangle inequality, any point is at least 2 away from `(0, 1)` and `(2, 1)` combined, and at least 2 away from `(1, 0)` and `(1, 2)` combined.
```text
Input: positions = [[1, 1], [3, 3]]
Output: 2.8284271247461903
```
Any point on the segment between the two customers gives the minimum, which is the distance between them, $\sqrt{8} \approx 2.82843$.
```text
Input: positions = [[0, 0], [0, 0], [3, 4]]
Output: 5.0
```
With the center on the two customers at `(0, 0)` the sum is 0 + 0 + 5 = 5. Moving it a distance $d$ away from the origin adds ${2d}$ for those two customers and, by the triangle inequality, saves at most $d$ on the third, so the best position is the shared customer location.
Overview: Given customer positions on a 2D map, compute the smallest possible sum of Euclidean distances from a single service center to every customer. It tests minimizing a convex function with an iterative method, choosing a numerical stopping rule, and handling a center that lands exactly on a customer.
Minimum sum of Euclidean distances from one service center to all customers
Citadel
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0
A delivery company wants to build a new service center in a new city. The company knows the positions of all the customers in this city on a 2D map and wants to build the new center in a position such that the sum of the Euclidean distances to all customers is minimum.
Given an array positions where positions[i] = [x_i, y_i] is the position of the i-th customer on the map, return the minimum sum of the Euclidean distances to all customers.
In other words, choose the position of the service center [x_centre, y_centre] so that the sum of Euclidean distances from the center to every customer is minimized. This is known as the geometric median problem.
The center at (1, 1) is at distance 1 from each customer. No point does better: by the triangle inequality, any point is at least 2 away from (0, 1) and (2, 1) combined, and at least 2 away from (1, 0) and (1, 2) combined.
With the center on the two customers at (0, 0) the sum is 0 + 0 + 5 = 5. Moving it a distance d away from the origin adds 2d for those two customers and, by the triangle inequality, saves at most d on the third, so the best position is the shared customer location.