Machine Learning
Topic 41 ยท Job ready ยท โฑ about 15 h

Python Coding Interview Practice

How coding interviews work, a method that works under pressure, Big-O basics, the common patterns, and 20 graded problems in the browser.

Show:
In this topic
Beginner

What to expect

Many intern and junior ML hiring processes include a general coding interview: 30โ€“60 minutes, one or two problems, either live with an interviewer or as an online test. For ML roles the problems are usually easy to medium (strings, lists, dictionaries, sorting, simple recursion), sometimes followed by an ML-flavoured task: implement a metric, k-means or a train/test split from scratch, or manipulate data with pandas.

Interviewers assess more than the final answer: how you clarify the problem, whether you test your code, how you handle edge cases, whether you can discuss efficiency, and how clearly you communicate while you work.

Beginner

A method that works under pressure

  1. Clarify: restate the problem; ask about input sizes, empty inputs, duplicates, negative numbers, ties.
  2. Examples: work one small example by hand, plus one edge case.
  3. Brute force first: say the simple solution and its cost, even if slow. A working slow answer beats an unfinished clever one.
  4. Improve: look for a pattern (below) that removes repeated work.
  5. Code: clear names, small helper functions, talk as you go.
  6. Test: run your examples and edge cases; trace through by hand if you can't run code.
  7. Analyse: state the time and space complexity.
Beginner

Big-O and the patterns that come up

Big-O describes how running time grows with input size nn, ignoring constants: O(1)O(1) constant (dictionary lookup), O(logโกn)O(\log n) (binary search), O(n)O(n) (one pass), O(nlogโกn)O(n\log n) (sorting), O(n2)O(n^2) (all pairs; nested loops). For nn = 1 million, O(n)O(n) is instant and O(n2)O(n^2) is far too slow.

  • Hash maps (dict/set) for "have I seen this?" and counting: turn O(n2)O(n^2) into O(n)O(n).
  • Sorting first, then a single pass.
  • Two pointers on sorted data (merging, pairs that sum to a target).
  • Sliding window for contiguous ranges (moving averages, longest substring).
  • Stack for nested structure (brackets, undo).
  • Recursion for nested or tree-shaped data.
  • Running summaries (prefix sums, Kadane's algorithm): keep a small state instead of re-scanning.
Try it

20 common interview problems, solved and explained

Twenty problems that come up often in coding interviews, from easy to medium, several of them ML-flavoured. Each one shows a clear solution, explained step by step, that you can run and change. When you are ready to practise under pressure, the game's coding tasks test you.

Interactive: this part runs in your browser.

Advanced

ML coding questions

Beyond general problems, ML interviews often ask you to implement something from scratch in NumPy in 20โ€“30 minutes. Practise these until they are routine (most appear somewhere in this course): accuracy, precision, recall and F1 from a confusion matrix; AUC by ranking; a train/test split with shuffling and a seed; standardisation; k-nearest neighbours; k-means (assignment and update steps); linear regression by gradient descent; logistic regression's sigmoid and log loss; softmax with the max-subtraction trick for stability; cosine similarity and top-k search. Say what you would test, and mention numerical issues (division by zero, log of zero, overflow) before the interviewer does.

Check yourself

Quiz

1. Checking whether a value is in a Python set isโ€ฆ

2. Two nested loops over n items cost aboutโ€ฆ

3. You can't see an efficient solution. In an interview you shouldโ€ฆ

4. Which pattern fits "the maximum sum of any 7 consecutive days"?

Go deeper

๐Ÿ“š Further reading

Trusted, mostly free sources: official documentation, well-known textbooks and courses. When you want to confirm or go deeper on anything here, start with these.

Ready