Partition One-Dimensional Points to Minimize Squared Distance
Company: LinkedIn
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Overview: Minimize one-dimensional squared-distance clustering cost with mean-based interval costs, prefix sums, and partition dynamic programming; compare k-means.
Read the full LinkedIn Machine Learning Engineer interview experience this question came from
Constraints
- 1 ≤ k ≤ n ≤ 2^31 − 1, where n is the number of positions.
- Each position is an integer from −(2^53 − 1) through 2^53 − 1, inclusive.
Examples
Input: ([0, 1], 1)
Expected Output: ('1', '2')
Explanation: The unrestricted center is 0.5, giving total squared error 0.5.
Input: ([8, 1, 9, 0], 2)
Expected Output: ('1', '1')
Explanation: The two adjacent pairs each contribute one half.
Hints
- The objective sums the squared distances for all positions. A center may lie between integer coordinates.