← TikTok Interview Insights

TikTok·Data Scientist·Technical Phone Screen·Senior

Senior
Jun 2026

Summary

TikTok data scientist interview that went deep into k-means from scratch, the kind of question where you think you know clustering until someone asks you to do it by hand with a specific seed and two full Lloyd iterations.

Questions Asked (4)

Q1

Given a specific set of 8 points in 2D space and k=2, walk through k-means++ initialization using a fixed random seed and Euclidean distance. Which centroids get sampled and why?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This part tripped me up more than I expected.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify that k-means++ selects initial centroids by weighted random sampling proportional to squared distances from existing centroids, and that a fixed seed makes the random choices deterministic. Then, walk through the algorithm step-by-step: compute distances from the first randomly chosen centroid, calculate probabilities, and use the seeded random number generator to pick subsequent centroids. Finally, explain why this initialization reduces the chance of poor clustering compared to random initialization.

Pro tip: Mention that the fixed seed ensures reproducibility, but the actual sampled centroids depend on the specific random number generator implementation (e.g., Python's random vs NumPy), so in practice you should specify the library and seed. This shows attention to detail and real-world implementation awareness.

1. State the k-means++ algorithm

Briefly outline the steps: choose first centroid uniformly at random, then for each subsequent centroid, compute D(x)^2 for each point (distance to nearest existing centroid squared), and sample with probability proportional to D(x)^2.

2. Apply fixed seed and first centroid selection

Explain that with a fixed seed, the first centroid is chosen deterministically from the uniform distribution over the 8 points. For example, if using Python's random with seed 42, the first index might be 3 (depending on implementation).

3. Compute distances and probabilities for second centroid

Calculate squared Euclidean distance from each point to the first centroid. Normalize these distances to sum to 1 to get probabilities. Then use the seeded random number generator to draw the second centroid according to these probabilities.

4. Identify the second centroid and explain why

Based on the computed probabilities and the random draw, determine which point is selected as the second centroid. Explain that points farther from the first centroid have higher probability, promoting spread.

5. Discuss implications and trade-offs

Highlight that k-means++ improves convergence and cluster quality, but the fixed seed means results are deterministic yet dependent on the seed. Mention that different seeds can lead to different initializations and final clusters.

Key Points to Mention

  • k-means++ selects initial centroids sequentially with probability proportional to squared distance from nearest existing centroid.
  • A fixed random seed makes the random choices reproducible, but the exact sampled points depend on the random number generator and seed value.
  • The first centroid is chosen uniformly at random from the dataset.
  • Squared Euclidean distance is used to compute probabilities, emphasizing spread.
  • k-means++ reduces the likelihood of poor initialization compared to random initialization, leading to better clustering.
  • In practice, specify the library (e.g., scikit-learn) and seed for reproducibility, and note that k-means++ is the default in many implementations.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Perform two full Lloyd iterations by hand on the initialized centroids: assign each point to its nearest centroid (breaking ties by lower index), then recompute centroids. Show cluster memberships and centroid coordinates after each iteration.

Algorithms & Data StructuresData Modeling
Author's notes

The tie-breaking rule is the kind of detail that sounds minor but will absolutely wreck your answer if you forget it mid-calculation.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, restate the initial centroids and data points to ensure clarity. Then, for each iteration, compute the Euclidean distance from each point to each centroid, assign points to the nearest centroid (breaking ties by lower index), and recompute centroids as the mean of assigned points. Present the results in a clear table showing cluster memberships and updated centroid coordinates after each iteration.

Pro tip: Mention that tie-breaking by lower index is a deterministic rule often used in implementations like scikit-learn to ensure reproducibility. Also, note that if a cluster becomes empty, you need a strategy (e.g., reinitialize or keep the centroid unchanged), though this may not occur in the given data.

1. Restate the problem setup

Clearly list the initial centroids and all data points with their coordinates. This ensures you and the interviewer are aligned on the starting conditions.

2. Iteration 1: Assign points to nearest centroid

For each point, calculate the Euclidean distance to each centroid. Assign the point to the cluster with the smallest distance, breaking ties by choosing the centroid with the lower index.

3. Iteration 1: Recompute centroids

For each cluster, compute the new centroid as the mean of the coordinates of all points assigned to it. Update the centroid coordinates accordingly.

4. Iteration 2: Assign points to nearest centroid

Using the updated centroids from Iteration 1, repeat the assignment step: compute distances and assign each point to the nearest centroid, again breaking ties by lower index.

5. Iteration 2: Recompute centroids

Recompute the centroids as the mean of points in each cluster after the second assignment. Present the final cluster memberships and centroid coordinates.

Key Points to Mention

  • Euclidean distance calculation: sqrt((x1-x2)^2 + (y1-y2)^2)
  • Tie-breaking rule: assign to the cluster with the lower centroid index
  • Centroid update: arithmetic mean of all points in the cluster
  • Convergence: Lloyd's algorithm iterates until assignments no longer change
  • Handling empty clusters: possible strategies if a cluster gets no points
  • Reproducibility: deterministic tie-breaking ensures consistent results

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

Compute the SSE (sum of squared errors) after the second Lloyd iteration.

Algorithms & Data StructuresProduct Analytics & Metrics
Author's notes

Straightforward once you have the final cluster assignments.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the dataset, initial centroids, and the definition of an iteration (assignment + update). Then, for the second iteration, reassign points to the nearest centroids from the first update, recompute centroids, and finally calculate SSE by summing squared distances from each point to its assigned centroid.

Pro tip: Mention that SSE is non-increasing across Lloyd iterations, so the second iteration's SSE should be less than or equal to the first. Also, if the question is ambiguous, state your assumptions clearly and proceed.

1. Clarify inputs and assumptions

Confirm the dataset points, initial centroids, and whether an iteration includes both assignment and update. If not provided, ask or state reasonable assumptions.

2. Perform first iteration (if needed)

If initial centroids are given, compute the first iteration: assign points to nearest centroids, then update centroids to the mean of assigned points.

3. Perform second iteration assignment

Using the updated centroids from the first iteration, reassign each point to the nearest centroid.

4. Update centroids after second assignment

Recompute each centroid as the mean of points assigned to it in the second iteration.

5. Compute SSE

For each point, calculate the squared Euclidean distance to its assigned centroid (after the second update) and sum all squared distances to get the SSE.

Key Points to Mention

  • Lloyd's algorithm alternates between assignment and update steps.
  • SSE is the sum of squared Euclidean distances from each point to its assigned centroid.
  • The second iteration uses centroids updated after the first iteration.
  • SSE is guaranteed to be non-increasing across iterations.
  • If clusters become empty, handle them appropriately (e.g., reinitialize or keep as is).
  • Clearly state any assumptions if the problem statement is ambiguous.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q4

How would you implement k-means efficiently in NumPy without looping over points? What is the time complexity per iteration, and how do you handle empty clusters robustly?

Algorithms & Data StructuresSystem DesignTechnical Trade-offs
Author's notes

This is where it got interesting.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining the vectorized assignment step using broadcasting to compute pairwise distances between all points and centroids, then the update step using one-hot encoding and matrix multiplication. Analyze time complexity per iteration as O(n*k*d) and discuss robust empty cluster handling via reassignment to farthest points or splitting largest clusters.

Pro tip: Mention that using squared Euclidean distances avoids the square root and is sufficient for assignment, and that for high-dimensional data, using the identity ||x-c||^2 = ||x||^2 - 2x·c + ||c||^2 can reduce computation via matrix multiplication.

1. Vectorized Distance Computation

Compute distances from all points to all centroids using broadcasting: diff = X[:, np.newaxis, :] - centroids[np.newaxis, :, :], then sum over the last axis. Alternatively, use the dot product trick for efficiency.

2. Assignment Step

Assign each point to the nearest centroid by taking argmin over the distance matrix: labels = np.argmin(distances, axis=1). This avoids explicit loops.

3. Update Step with One-Hot Encoding

Create a one-hot matrix of labels (n x k), then compute new centroids as (one_hot.T @ X) / counts, where counts = one_hot.sum(axis=0). Handle division by zero for empty clusters.

4. Empty Cluster Handling

Detect empty clusters (counts == 0) and reassign them to the point farthest from its assigned centroid, or split the cluster with the highest variance. Update centroids accordingly.

5. Complexity Analysis

State that per iteration, distance computation is O(n*k*d), assignment O(n*k), and update O(n*k + n*d). Overall O(n*k*d) per iteration, which is optimal for exact k-means.

Key Points to Mention

  • Use of broadcasting and vectorized operations to avoid Python loops.
  • Squared Euclidean distance for efficiency (no sqrt needed for argmin).
  • One-hot encoding and matrix multiplication for centroid update.
  • Time complexity per iteration: O(n*k*d) where n=points, k=clusters, d=dimensions.
  • Empty cluster strategies: reassign to farthest point, split largest cluster, or reinitialize randomly.
  • Potential memory optimization: compute distances in chunks if n*k*d is too large.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.