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"
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
| Rank | Topic | Frequency | Why It Matters for ML |
|---|---|---|---|
| 1 | Hash Maps | Very High | Feature lookups, counting, deduplication |
| 2 | Arrays / Two Pointers | Very High | Data manipulation, batch processing |
| 3 | Trees (BST, BFS, DFS) | High | Decision trees, hierarchical data |
| 4 | Graphs (BFS/DFS) | High | Knowledge graphs, dependency DAGs |
| 5 | Sorting / Searching | High | Top-K problems, data pipeline operations |
| 6 | Sliding Window | High | Time series, streaming data |
| 7 | Heap / Priority Queue | Medium | Top-K, median finding, beam search |
| 8 | Dynamic Programming | Medium | Sequence alignment, edit distance |
| 9 | Linked Lists | Medium | Less common for ML roles |
| 10 | Tries | Low | Text autocomplete, NLP preprocessing |
Target Problem Count for Prep
| Level | Minimum | Comfortable | Ready |
|---|---|---|---|
| Easy | 30 | 50 | 75+ |
| Medium | 50 | 100 | 150+ |
| Hard | 10 | 25 | 50+ |
Part 3 - The Problem-Solving Framework
Step-by-Step
- Read & Understand: Read the problem twice. Don't start solving yet.
- 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?"
- Brute Force: State the naive approach and its complexity. "The brute force is O(n²) - check every pair."
- 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)."
- Code: Write clean code. Use meaningful names. Think out loud.
- Test: Walk through with a small example. Check edge cases (empty input, single element, duplicates).
Common Failure Modes
| Failure Mode | What Happens | How to Avoid |
|---|---|---|
| Silent coding | You code without talking; interviewer can't help | Think out loud. Narrate every decision. |
| Premature coding | You start coding before understanding the problem | Force yourself: 5 minutes of discussion before first line of code |
| Perfectionism | You refuse to write brute force first | Start simple. A working O(n²) solution beats a broken O(n) solution. |
| Ignoring hints | Interviewer offers a hint and you dismiss it | Hints are gifts. Take them immediately. |
| No testing | You finish coding and say "done" | Always walk through your code with a test case |
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
| Algorithm | Key Components | Common Bugs |
|---|---|---|
| Linear Regression | MSE loss, gradient computation, parameter update | Forgetting to add bias, wrong gradient sign |
| Logistic Regression | Sigmoid, binary cross-entropy, gradient update | Numerical overflow in sigmoid, wrong loss formula |
| K-Means | Random init, assign clusters, update centroids, repeat | Not handling empty clusters, convergence check |
| Decision Tree | Information gain / Gini, recursive split, stopping criteria | Stack overflow without depth limit |
| KNN | Distance computation, sorting, majority vote | Not handling ties, wrong distance metric |
| Neural Network | Forward pass, loss, backward pass, weight update | Wrong chain rule application, forgetting to zero gradients |
| AUC-ROC | Sort by score, compute TPR/FPR at each threshold, trapezoidal area | Off-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
| Company | Coding Round Details |
|---|---|
| 2 coding rounds. LeetCode Medium-Hard. Very strong bar. Must finish both problems. | |
| Meta | 2 coding rounds. LeetCode Medium. Speed matters - some interviewers give 2 problems per round. |
| Amazon | 1-2 coding rounds. LeetCode Medium. Always includes LP behavioral component. |
| OpenAI/Anthropic | 1-2 rounds. May include "implement this paper" style ML coding. |
| Startups | 1 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
| Situation | Tactic |
|---|---|
| Don't recognize the pattern | Start with brute force. The pattern often becomes clear as you code. |
| Stuck for 5+ minutes | Ask: "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 code | Stay calm. Walk through line by line with a test case. Most bugs are off-by-one or wrong variable. |
| Run out of time | Describe 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
- ML Knowledge Round - The theory depth round
- System Design Round - The architecture round
- For focused coding prep → Coding Interviews
