Prepin
Log in

M

L

Execute k-means clustering.

Approach / Explanation

This question requires you to implement the k-means clustering algorithm, which is a form of unsupervised machine learning. The core concept of the k-means algorithm is to partition data points into k distinct clusters, with each point assigned to the nearest mean (the 'centroid' of a cluster). To address this question, you should follow these steps: Grasp the theoretical foundations of the k-means clustering algorithm. Begin with an initial set of means and classify each data point by identifying the closest mean. Calculate new means by averaging the data points within each cluster. Check if the means affect the classification of any data points. If they do, return to step 2; if not, conclude the process. Having a comprehensive understanding of k-means clustering, both from a theoretical and practical perspective, along with knowledge of relevant terms and parameters, is crucial.

Suggested Answer

Below is a straightforward implementation of the k-means algorithm in Python: import numpy as np def kmeans ( data , k , max_iters = 100 ) : # Randomly initialize centroids centroids = data [ np . random . choice ( range ( data . shape [ 0 ] ) , size = k , replace = False ) ] for _ in range ( max_iters ) : # Assign each data point to the nearest centroid labels = np . argmin ( np . linalg . norm ( data [ : , np . newaxis ] - centroids , axis = - 1 ) , axis = - 1 ) # Recalculate centroids based on the clusters new_centroids = np . array ( [ data [ labels == i ] . mean ( axis = 0 ) for i in range ( k ) ] ) # Check if centroids have converged if np . all ( centroids == new_centroids ) : break centroids = new_centroids return centroids , labels This code begins by randomly selecting data points to serve as the centroids. It then loops through a predetermined maximum count of iterations, during which each point is assigned to the closest centroid, followed by a recalculation of the centroid positions. This cycle continues until either the maximum iterations are completed or the centroids no longer change, indicating convergence. Note: It is essential to understand that this is a basic implementation of k-means. In practical scenarios, you may need to incorporate additional features, such as eliminating empty clusters or stopping if the movement of any centroid is below a specified threshold.

Alternative Answer

If your interviewer asks about implementing k-means with a pre-existing machine learning library such as scikit-learn, you can refer to the implementation provided above. This approach is particularly advantageous in an interview setting as it allows you to save time and keeps your code neat. However, make sure you grasp the underlying processes, as the interviewer might inquire about how it functions.

Question Details

Difficulty & Category

Medium
analytical

Companies

Amazon
Meta (Facebook)
LinkedIn

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.