Split Store Locations Into K Clusters With Maximum Separation

Last updated: November 29, 2026

Quick Overview

Given 2D coordinates of store locations and an integer k, partition the stores into exactly k non-empty clusters to maximize the spacing, defined as the smallest distance between two stores in different clusters. Return that maximum spacing.

Google
Coding & Algorithms
Machine Learning Engineer
Google
November 29, 2026
Machine Learning Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given 2D coordinates of store locations and an integer k, partition the stores into exactly k non-empty clusters to maximize the spacing, defined as the smallest distance between two stores in different clusters. Return that maximum spacing.

Google Maps and Ads use geographic clustering, and single-linkage clustering is exactly Kruskal stopped early. Interviewers use this to see whether you connect an ML-flavored problem to a classic algorithm and can explain why stopping at k components is optimal.

What the Interviewer Expects
  • Recognizes single-linkage clustering as truncated Kruskal
  • Sorts all pairwise distances
  • Merges until exactly k clusters remain
  • Returns the next crossing edge as the spacing
  • Discusses the O(n^2 log n) cost and how to reduce it
Key Topics to Cover
Union-Find
Kruskal
Single-Linkage Clustering
Greedy
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you scale this to a million points?
  • What are the weaknesses of single linkage compared with k-means?
  • Return the cluster labels as well as the spacing.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: list of (x, y) points and 1 <= k <= n.
  • Distance is Euclidean.
  • Output: maximum achievable minimum inter-cluster distance (infinity when k = 1, since nothing crosses)....
High-Level Design

Sort all pairs by distance and merge greedily, like Kruskal's algorithm, until k clusters remain. The next edge that would join two different clusters is the spacing. This is optimal: any other k-part...


Submit Your Answer
Markdown supported