PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Adobe

Implement K-means clustering from scratch

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's understanding of clustering algorithms and practical implementation skills in unsupervised machine learning, including distance computations, centroid estimation, and handling edge cases like empty clusters.

  • medium
  • Adobe
  • Coding & Algorithms
  • Machine Learning Engineer

Implement K-means clustering from scratch

Company: Adobe

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Task Implement **K-means clustering** from scratch (no ML libraries). You are given a dataset of points: - Input: an array `X` of shape `(n, d)` (n points, each d-dimensional) - An integer `k` (number of clusters) - `max_iters` and a convergence tolerance `tol` ### Requirements 1. Initialize `k` centroids (you may choose a standard approach such as random points from `X`). 2. Repeat until convergence or `max_iters`: - **Assignment step:** assign each point to its nearest centroid (e.g., Euclidean distance). - **Update step:** recompute each centroid as the mean of points assigned to it. 3. Handle the edge case where a cluster becomes empty. ### Output Return: - `centroids`: array of shape `(k, d)` - `labels`: array of length `n` with cluster indices `0..k-1` ### Follow-ups (discussion) - Time complexity per iteration. - How initialization affects convergence and quality (e.g., K-means++ idea).

Quick Answer: This question evaluates a candidate's understanding of clustering algorithms and practical implementation skills in unsupervised machine learning, including distance computations, centroid estimation, and handling edge cases like empty clusters.

Related Interview Questions

  • Traverse a path and print directory tree - Adobe (medium)
  • Build a React team builder with role constraints - Adobe (medium)
  • Design a nested-list iterator - Adobe (Medium)
  • Maximize pay by flipping k rest days - Adobe (Medium)
  • Determine task order with prerequisites - Adobe (Medium)
Adobe logo
Adobe
Jan 13, 2026, 12:00 AM
Machine Learning Engineer
Technical Screen
Coding & Algorithms
5
0
Loading...

Task

Implement K-means clustering from scratch (no ML libraries).

You are given a dataset of points:

  • Input: an array X of shape (n, d) (n points, each d-dimensional)
  • An integer k (number of clusters)
  • max_iters and a convergence tolerance tol

Requirements

  1. Initialize k centroids (you may choose a standard approach such as random points from X ).
  2. Repeat until convergence or max_iters :
    • Assignment step: assign each point to its nearest centroid (e.g., Euclidean distance).
    • Update step: recompute each centroid as the mean of points assigned to it.
  3. Handle the edge case where a cluster becomes empty.

Output

Return:

  • centroids : array of shape (k, d)
  • labels : array of length n with cluster indices 0..k-1

Follow-ups (discussion)

  • Time complexity per iteration.
  • How initialization affects convergence and quality (e.g., K-means++ idea).

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Adobe•More Machine Learning Engineer•Adobe Machine Learning Engineer•Adobe Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.