Skip to main content

The Coding Round - The Universal Gate

Reading time: ~20 min | Interview relevance: Critical | Roles: All

The Real Interview Moment

You're in the first on-site round. The interviewer says: "Given a stream of numbers, implement a class that can efficiently return the median at any time." You know this is a heap problem, but under pressure your implementation of the two-heap approach has a bug in the rebalancing logic. You spend 15 minutes debugging. The interviewer tries to help, but you're flustered and miss the hint. You get a "Lean No Hire" - and this single round torpedoes your otherwise strong packet.

The coding round is the most common reason strong ML candidates fail interviews. You can ace system design and ML depth, but if you can't code under pressure, you won't get an offer at any top company.

What You Will Master

  • How AI/ML coding rounds differ from standard SWE coding rounds
  • The two types: DSA coding and ML-specific coding
  • A systematic approach to solving problems under time pressure
  • The most tested topics and problem patterns
  • How to handle getting stuck, bugs, and hints

Part 1 - Two Types of Coding Rounds

Type 1: DSA Coding (Standard)

Same as SWE interviews. LeetCode Medium is the target difficulty. You have 45 minutes to solve 1-2 problems.

Type 2: ML-Specific Coding

Unique to ML roles. You implement ML algorithms from scratch using Python + NumPy. Examples:

  • "Implement logistic regression with gradient descent"
  • "Implement k-means clustering"
  • "Implement a simple neural network (forward + backward pass)"
  • "Write a function that computes AUC-ROC from scratch"
  • "Implement TF-IDF"
Interviewer's Perspective

In the ML coding round, I'm testing whether you understand what's happening under the hood. If you can train a model with scikit-learn but can't implement the gradient update step, you're using tools without understanding them. That's fine for junior roles, but Senior+ MLEs should be able to derive and implement core algorithms from scratch.

Part 2 - DSA Topics Ranked by Frequency

For AI/ML Roles Specifically

RankTopicFrequencyWhy It Matters for ML
1Hash MapsVery HighFeature lookups, counting, deduplication
2Arrays / Two PointersVery HighData manipulation, batch processing
3Trees (BST, BFS, DFS)HighDecision trees, hierarchical data
4Graphs (BFS/DFS)HighKnowledge graphs, dependency DAGs
5Sorting / SearchingHighTop-K problems, data pipeline operations
6Sliding WindowHighTime series, streaming data
7Heap / Priority QueueMediumTop-K, median finding, beam search
8Dynamic ProgrammingMediumSequence alignment, edit distance
9Linked ListsMediumLess common for ML roles
10TriesLowText autocomplete, NLP preprocessing

Target Problem Count for Prep

LevelMinimumComfortableReady
Easy305075+
Medium50100150+
Hard102550+

Part 3 - The Problem-Solving Framework

Coding Round - Problem-Solving Framework

Step-by-Step

  1. Read & Understand: Read the problem twice. Don't start solving yet.
  2. Clarify: Ask about edge cases, constraints, input size. "Can the input be empty? Are there negative numbers? What should I return if no solution exists?"
  3. Brute Force: State the naive approach and its complexity. "The brute force is O(n²) - check every pair."
  4. Optimize: Think about better data structures. "If I sort first, I can use binary search. Or with a hash map, I can do it in O(n)."
  5. Code: Write clean code. Use meaningful names. Think out loud.
  6. Test: Walk through with a small example. Check edge cases (empty input, single element, duplicates).

Common Failure Modes

Failure ModeWhat HappensHow to Avoid
Silent codingYou code without talking; interviewer can't helpThink out loud. Narrate every decision.
Premature codingYou start coding before understanding the problemForce yourself: 5 minutes of discussion before first line of code
PerfectionismYou refuse to write brute force firstStart simple. A working O(n²) solution beats a broken O(n) solution.
Ignoring hintsInterviewer offers a hint and you dismiss itHints are gifts. Take them immediately.
No testingYou finish coding and say "done"Always walk through your code with a test case
Common Trap

Many ML candidates skip DSA prep because "I'm not a SWE, I'm an MLE." Then they fail the coding round and the hiring committee rejects their packet despite strong ML and design rounds. The coding round is a veto gate. A "Strong No Hire" here kills your candidacy. Budget at least 25% of your prep time for DSA, even if you're an experienced MLE.

Part 4 - ML-Specific Coding Problems

The Must-Know Implementations

AlgorithmKey ComponentsCommon Bugs
Linear RegressionMSE loss, gradient computation, parameter updateForgetting to add bias, wrong gradient sign
Logistic RegressionSigmoid, binary cross-entropy, gradient updateNumerical overflow in sigmoid, wrong loss formula
K-MeansRandom init, assign clusters, update centroids, repeatNot handling empty clusters, convergence check
Decision TreeInformation gain / Gini, recursive split, stopping criteriaStack overflow without depth limit
KNNDistance computation, sorting, majority voteNot handling ties, wrong distance metric
Neural NetworkForward pass, loss, backward pass, weight updateWrong chain rule application, forgetting to zero gradients
AUC-ROCSort by score, compute TPR/FPR at each threshold, trapezoidal areaOff-by-one in threshold loop

Example: Implement Logistic Regression from Scratch

Full Implementation + Rubric
import numpy as np

class LogisticRegression:
def __init__(self, lr=0.01, n_iters=1000):
self.lr = lr
self.n_iters = n_iters
self.weights = None
self.bias = None

def sigmoid(self, z):
return 1 / (1 + np.exp(-np.clip(z, -500, 500)))

def fit(self, X, y):
n_samples, n_features = X.shape
self.weights = np.zeros(n_features)
self.bias = 0

for _ in range(self.n_iters):
linear = X @ self.weights + self.bias
predictions = self.sigmoid(linear)

dw = (1 / n_samples) * (X.T @ (predictions - y))
db = (1 / n_samples) * np.sum(predictions - y)

self.weights -= self.lr * dw
self.bias -= self.lr * db

def predict(self, X):
linear = X @ self.weights + self.bias
probabilities = self.sigmoid(linear)
return (probabilities >= 0.5).astype(int)

Scoring:

  • Strong Hire: Correct implementation, clips sigmoid to avoid overflow, explains gradient derivation, tests with example
  • Lean Hire: Correct but doesn't handle numerical stability
  • No Hire: Can't derive the gradient or produces incorrect implementation

Part 5 - Company Variations

CompanyCoding Round Details
Google2 coding rounds. LeetCode Medium-Hard. Very strong bar. Must finish both problems.
Meta2 coding rounds. LeetCode Medium. Speed matters - some interviewers give 2 problems per round.
Amazon1-2 coding rounds. LeetCode Medium. Always includes LP behavioral component.
OpenAI/Anthropic1-2 rounds. May include "implement this paper" style ML coding.
Startups1 round. Often practical - "build a data pipeline" or "implement this feature."

Practice Problems

Problem 1: ML-Flavored DSA

Given a dataset of points (x, y) and a new query point, return the k nearest neighbors using Euclidean distance.

Hint 1 - Direction

Compute distances, then use a heap (min-heap of size k) for O(n log k) efficiency.

Full Answer + Rubric
import heapq
import math

def knn(points, query, k):
# Max heap of size k (negate distances for max-heap behavior)
heap = []
for point in points:
dist = math.sqrt((point[0] - query[0])**2 + (point[1] - query[1])**2)
if len(heap) < k:
heapq.heappush(heap, (-dist, point))
elif -dist > heap[0][0]:
heapq.heapreplace(heap, (-dist, point))
return [point for _, point in heap]

Scoring:

  • Strong Hire: O(n log k) with heap, handles edge cases, clean code
  • Lean Hire: O(n log n) with full sort - correct but not optimal
  • No Hire: O(nk) or can't implement distance correctly

Interview Cheat Sheet

SituationTactic
Don't recognize the patternStart with brute force. The pattern often becomes clear as you code.
Stuck for 5+ minutesAsk: "Can I think about this from a different angle?" Try a different data structure.
Interviewer gives a hint"That's a great point - let me think about how [hint] changes my approach."
Bug in your codeStay calm. Walk through line by line with a test case. Most bugs are off-by-one or wrong variable.
Run out of timeDescribe your remaining approach verbally. "Given 5 more minutes, I'd add handling for X."

Spaced Repetition Checkpoints

  • Day 0: Solve 3 LeetCode Mediums. Time yourself (target: 25 min each).
  • Day 3: Implement logistic regression from scratch. Without looking at any reference.
  • Day 7: Solve 5 more LeetCode Mediums across different topics (trees, graphs, DP).
  • Day 14: Do a mock coding interview with a friend. 45 minutes, 1 problem + discussion.
  • Day 21: Implement K-Means and a simple neural network from scratch.

What's Next

© 2026 EngineersOfAI. All rights reserved.