Coding Interviews
Source: Concepts adapted from Ace the Data Science Interview — Chapter 9 (“Coding”) by Kevin Huo & Nick Singh. Rewritten in original wording for this course.
Data scientists write code constantly — wrangling frames, calling APIs, shipping pipelines. Interviews therefore often revisit data structures and algorithms: lists, trees, graphs, recursion, dynamic programming, and Big-O for time and space. Even if your day job is notebooks, shared fluency with engineers matters.
This lesson covers how coding screens usually run and how to talk about complexity. Later lessons drill data structures and algorithms.
How coding rounds are run
Remote screens often use HackerRank, Codility, or CoderPad; onsites may use a whiteboard. The same playbook applies either way.
After you hear the prompt
- Restate the problem and confirm I/O types, ranges, and whether inputs are well-formed.
- Walk a small example end-to-end before writing code.
- Only then start designing a solution.
While designing
- Sketch a correct brute force first and say why it is slow.
- Improve it if you can, and explain the trade-off (time vs space, simplicity vs optimality).
- Code only after you and the interviewer agree on an approach.
While coding
- Narrate as you go; use real language (not vague pseudocode) unless they ask for high-level only.
- Prefer clear names and structure; offer to fill in deferred helpers later.
After coding
- Hunt edge cases, then dry-run tests.
- Expect follow-ups: complexity, cleanup, “do it in-place,” “no recursion,” or a harder variant.
Big-O in interviews
Asymptotic analysis describes how time and memory grow as input size . Industry interviews usually mean worst-case upper bounds when they say “Big-O.”
Common classes (fast → slow for typical array problems):
| Class | Name | Typical example |
|---|---|---|
| Constant | Index into an array | |
| Logarithmic | Binary search on sorted data | |
| Linear | Single pass | |
| Log-linear | Mergesort | |
| Quadratic | Nested loops over pairs | |
| Exponential | Naïve recursion over binary choices | |
| Factorial | All permutations |
Space works the same way: copying an -element array costs extra space; an adjacency matrix costs .
Binary search sketch: each step halves the remaining range → time. The input array itself is space; the algorithm needs only a few indices → auxiliary space.
Complexity of simple ML algorithms
Interviewers sometimes ask you to reason about training cost.
- Naive Bayes (roughly): for points, features, classes, counting co-occurrences is about time; storing per-class feature stats is about space.
- Logistic regression (high level): each update touches a -dimensional weight vector; space for weights is . Exact training cost depends on iterations and solver, but the “per-example multiply by ” intuition is work per point.
Use these as order-of-magnitude stories, not as claims that every library implementation matches the textbook loop.
Interview process & Big-O
1 / 4Right after receiving a coding prompt, the best first move is usually…
Pick an answer to see if you are right.
Practice: binary search closest index
Implement a binary search that returns the index of k if present, otherwise the index of the closest value (ties: prefer the leftmost among equally close).
Next: Data structures.