Algorithms and coding
Tick a problem or variation once you can solve it without the hint. Ticks stay in this browser; nothing is sent anywhere.
How to code in an interview
- Restate the problem in one sentence, and ask about input size, empty input, duplicates and negative numbers.
- Say the brute force and its cost before anything clever. It's a correct answer you can improve.
- Name the pattern ("this is a sliding window") and the better complexity.
- Write it while talking. Clear names over short ones. Syntax matters less than structure.
- Run one small example by hand, then the edge cases: empty, one element, all the same.
- State time and space at the end, and one way you'd improve it if they asked.
Complexity cheat sheet
| Operation | Cost |
|---|---|
| dict / set lookup, insert | O(1) on average |
| list append, pop from the end | O(1) |
| list insert or pop at the front | O(n): use collections.deque for O(1) |
| sort | O(n log n) |
| heapq push / pop | O(log n); heapify O(n) |
| binary search | O(log n) on sorted data |
| BFS / DFS on a graph | O(V + E) |
| n up to 10^5 or 10^6 | aim for O(n) or O(n log n) |
| n up to about 20 | O(2^n) backtracking is fine |