K-Means Clustering
Updated
Problem statement
In a k-means clustering problem, a dataset contains n data points, where nth data point is represented by the feature vector location[i]. The goal is to create k clusters, where the cluster centers or the cluster centroids can be placed at any point in the feature space. The overall quality of the clustering is measured by the maximum distance between any data point and its nearest cluster center.
The best possible quality is achieved by optimally placing the cluster centers to minimize this maximum distance. Determine this maximum distance between any data point and its nearest cluster center.
Note: The distance between two feature points x and y is defined as |x-y|, where |x| denotes the absolute value of x.
Function
getMaximumDistance(location: int[], k: int) → int
Complete the function getMaximumDistance in the editor below.
getMaximumDistance takes the following parameters:
int location[n]: the feature locations of all the data pointsint k: the number of clusters
Returns
int: the maximum distance between any data point and its nearest cluster center by optimally placing the k clusters
Examples
Example 1
location = [4, 1, 6, 7, 2]k = 2return = 2The maximum of all distances is 2.
Constraints
1 ≤ n ≤ 10^51 ≤ k ≤ n1 ≤ location[i] ≤ 10^9
More Atlassian interview questions
- Merge Overlapping IntervalsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Tennis Court SchedulingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Deterministic Word Guess SolverONSITE INTERVIEW · Seen Oct 2026
- Generate Word Guess FeedbackONSITE INTERVIEW · Seen Oct 2026
- Bounded Word Guess SolverONSITE INTERVIEW · Seen Oct 2026
- Implement a Rate LimiterPHONE SCREEN · Seen Oct 2026
- Word Guess Feedback ColorsONSITE INTERVIEW · Seen Oct 2026
- Longest Consistent Activity StreakPHONE SCREEN · Seen Sep 2026
- Music Player Operation SequencePHONE SCREEN · Seen Sep 2026
- Underline Shortest Unique SubstringsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Capacity to Ship Packages Within DaysMicrosoft · OA · ONSITE INTERVIEWMicrosoft interview questions
- Rock JumpingIMC Trading · OAIMC Trading interview questions
- Minimum Robot and Human Fulfillment TimeAmazon · OA
- Minimize Maximum Group DifferenceDRW · OADRW interview questions
- Array NullificationQRT · OA
- Minimum-Cost Solar Rooftop AllocationDeloitte · OADeloitte interview questions
- Three-Part Partition with Minimum Largest RangeLSEG · OALSEG interview questions
- Get Minimum Development TimeSalesforce · OASalesforce interview questions
- Job ExecutionMathWorks · OAMathWorks interview questions
- Police Station 🚓The D.E. Shaw Group · OAThe D.E. Shaw Group interview questions