PracHub
QuestionsLearningGuidesInterview Prep
|Home/Machine Learning/Lyft

Implement K-Means Without Numerical Libraries

Last updated: Jul 22, 2026

Quick Overview

Implement K-means clustering from first principles with fixed initialization, tie handling, convergence rules, and an explicit empty-cluster policy. The exercise covers the full assignment-update loop, numerical choices, complexity, and edge-case testing without numerical libraries.

  • medium
  • Lyft
  • Machine Learning
  • Machine Learning Engineer

Implement K-Means Without Numerical Libraries

Company: Lyft

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Technical Screen

# Implement K-Means Without Numerical Libraries Implement K-means clustering for a list of finite numeric points using only core language features and elementary arithmetic. The function receives the initial centroids so that initialization is deterministic. ```python def kmeans(points, initial_centroids, max_iterations, tolerance): ... ``` Return the final centroids and one cluster index per input point. Use squared Euclidean distance. On an exact distance tie, choose the lower centroid index. Stop when either the iteration limit is reached or the largest centroid movement is at most `tolerance`. State and implement a deterministic policy for an empty cluster. ### Constraints & Assumptions - All points and centroids have the same positive dimension. - `1 <= k <= number of points`. - Inputs contain only finite numbers. - The returned cluster indices must correspond to the returned centroids; perform a final assignment if the last update moved a centroid. ### Clarifying Questions to Ask - Should movement use Euclidean distance or squared distance? - What empty-cluster policy is expected? - Is deterministic output required for tests? - Are duplicate points and duplicate starting centroids allowed? ### What a Strong Answer Covers - A complete assign-update loop with explicit termination - Stable, deterministic tie and empty-cluster behavior - Avoiding unnecessary square roots during assignment - Complexity in terms of points, clusters, dimensions, and iterations - Tests for duplicate points, empty clusters, convergence, and iteration limits ### Follow-up Questions - Why does standard K-means reduce its objective on each non-empty update step? - How would K-means++ change initialization? - What makes the result sensitive to scale and outliers? - How would you process data that cannot fit in memory?

Quick Answer: Implement K-means clustering from first principles with fixed initialization, tie handling, convergence rules, and an explicit empty-cluster policy. The exercise covers the full assignment-update loop, numerical choices, complexity, and edge-case testing without numerical libraries.

Related Interview Questions

  • Reason About an Intercept Shift in Logistic Regression - Lyft (medium)
  • Explain Transformer and Fine-Tuning Basics - Lyft (medium)
  • Develop Dynamic-Pricing Algorithm for Lyft Balancing Key Factors - Lyft (hard)
|Home/Machine Learning/Lyft

Implement K-Means Without Numerical Libraries

Lyft logo
Lyft
Jul 4, 2026, 12:00 AM
mediumMachine Learning EngineerTechnical ScreenMachine Learning
0
0

Implement K-Means Without Numerical Libraries

Implement K-means clustering for a list of finite numeric points using only core language features and elementary arithmetic. The function receives the initial centroids so that initialization is deterministic.

def kmeans(points, initial_centroids, max_iterations, tolerance):
    ...

Return the final centroids and one cluster index per input point. Use squared Euclidean distance. On an exact distance tie, choose the lower centroid index. Stop when either the iteration limit is reached or the largest centroid movement is at most tolerance. State and implement a deterministic policy for an empty cluster.

Constraints & Assumptions

  • All points and centroids have the same positive dimension.
  • 1 <= k <= number of points .
  • Inputs contain only finite numbers.
  • The returned cluster indices must correspond to the returned centroids; perform a final assignment if the last update moved a centroid.

Clarifying Questions to Ask Guidance

  • Should movement use Euclidean distance or squared distance?
  • What empty-cluster policy is expected?
  • Is deterministic output required for tests?
  • Are duplicate points and duplicate starting centroids allowed?

What a Strong Answer Covers Guidance

  • A complete assign-update loop with explicit termination
  • Stable, deterministic tie and empty-cluster behavior
  • Avoiding unnecessary square roots during assignment
  • Complexity in terms of points, clusters, dimensions, and iterations
  • Tests for duplicate points, empty clusters, convergence, and iteration limits

Follow-up Questions Guidance

  • Why does standard K-means reduce its objective on each non-empty update step?
  • How would K-means++ change initialization?
  • What makes the result sensitive to scale and outliers?
  • How would you process data that cannot fit in memory?
Loading comments...

Browse More Questions

More Machine Learning•More Lyft•More Machine Learning Engineer•Lyft Machine Learning Engineer•Lyft Machine Learning•Machine Learning Engineer Machine Learning

Write your answer

Your first approved answer each day earns 20 XP.

Sign in to write your answer.
PracHub

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

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding 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.