K-Means Clustering Implementation

K-Means Clustering Implementation

Implement the K-Means clustering algorithm to partition a set of N points into K clusters. You are given: points: A list of D-dimensional coordinates (as tuples). k: The number of clusters to identify. initial_centroids: A list of K points representing the starting positions of the centroids. max_iterations: The maximum number of assignment/update cycles to perform. Your algorithm should follow these steps: Assignment: Assign each point to the cluster associated with the nearest centroid using squared Euclidean distance. Update: Recompute the centroid of each cluster by taking the arithmetic mean of all points assigned to that cluster. If a cluster has no assigned points, its centroid remains unchanged for that iteration. Convergence: If the centroids do not change between two consecutive iterations, terminate early. The final output should be a list of the K cluster centroids, with each coordinate rounded to exactly 4 decimal places.
PythonK-Means ClusteringEuclidean Distance
01
Read
1
InterviewGPT

AI-powered tools to help you succeed in tech interviews — from resume to offer.

Interview Solver

  • Coding Puzzles
  • System Design
  • Behavioral Challenges
  • ML System Design
  • SQL Puzzles
  • FE System Design
Explore Solver

Question Bank

  • Coding Interview Questions
  • System Design Interview Questions
  • Behavioral Interview Questions
  • ML System Design Questions
  • SQL & Database Questions
  • FE System Design Questions
Explore Questions

Golden Blogs

  • Coding Solutions
  • System Design Guides
  • Behavioral Guides
  • ML System Design Guides
  • SQL Solutions
  • FE System Design Guides
Explore Blogs

Intervipedia

  • Coding Concepts
  • System Design Concepts
  • Behavioral Concepts
  • ML System Concepts
  • SQL Concepts
  • FE System Concepts
Explore Concepts

Application Tools

  • Self-Intro Generator

Company

  • Pricing
  • FAQ
  • About
  • Privacy Policy
  • Terms of Service

© 2026 InterviewGPT Inc. All rights reserved.

All systems operationalUS-East

Made with ♥ for developers