Magicsheet logo

Best Position for a Service Centre

Hard
74.4%
Updated 6/1/2025

Asked by 2 Companies

Best Position for a Service Centre

What is this problem about?

The "Best Position for a Service Centre interview question" is a classic geometric optimization challenge. You are given the (x,y)(x, y) coordinates of several customers. You need to find a new coordinate (xcenter,ycenter)(x_{center}, y_{center}) such that the sum of the Euclidean distances from this center to all customers is minimized. This is a well-known mathematical problem called the "Geometric Median" or the "Fermat-Weber point."

Why is this asked in interviews?

Uber and Citadel ask the "Best Position for a Service Centre coding problem" to test a candidate's understanding of Numerical Optimization and Iterative Algorithms. Since there is no simple closed-form formula for the geometric median of more than four points, you must use an iterative approach like "Weiszfeld's algorithm" or a "Randomized/Gradient Descent" search. It evaluates advanced "Math interview pattern" and "Geometry" skills.

Algorithmic pattern used

This problem is typically solved using Gradient Descent or Ternary Search (for each dimension).

  1. Initial Point: Start at the "mean" of all customer coordinates (the average XX and average YY).
  2. Iterative Step:
    • Choose a step size (e.g., 100).
    • Check if moving in any of the four directions (Up, Down, Left, Right) reduces the total distance sum.
    • If a better point is found, move there and repeat.
    • If no better point is found in any direction, reduce the step size (e.g., divide by 10) and try again.
  3. Termination: Stop when the step size becomes extremely small (e.g., 10710^{-7}), ensuring high precision.

Example explanation

Customers: (0,1), (1,0), (1,2), (2,1)

  1. Start at center (1,1). Total distance: 1+1+1+1=41 + 1 + 1 + 1 = 4.
  2. Check (1.1, 1): Total distance increases.
  3. Check (0.9, 1): Total distance increases.
  4. The center (1,1) is already optimal for this symmetric set. For non-symmetric sets, the algorithm would "crawl" toward the point that minimizes the total sum.

Common mistakes candidates make

  • Using Mean: Thinking the average XX and YY is the optimal point. While the mean minimizes squared distances, it does not minimize absolute Euclidean distances.
  • Precision: Not iterating enough times to reach the required decimal precision (usually 10510^{-5}).
  • Step Size: Not decreasing the step size when the search gets stuck, causing the algorithm to "jump over" the optimal point.

Interview preparation tip

Numerical optimization is rare in interviews but critical for specialized roles in finance or logistics. Practice "Hill Climbing" or simple gradient descent logic. Remember that for convex functions (like this one), any local minimum is also the global minimum.

Similar Questions