Cheat Sheets
Python Coding Interview Questions · Study material
Coding Interview Cheat Sheet
| Need | Python | Cost |
|---|---|---|
| Fast membership | seen = set() | O(1) |
| Counting | Counter(items), d.get(k, 0) + 1 | O(n) |
| Grouping | defaultdict(list) | O(n) |
| Queue / BFS | deque, append, popleft | O(1) |
| Stack | list.append, list.pop | O(1) |
| Priority queue | heapq.heappush(h, (p, x)), heappop | O(log n) |
| Top k | heapq.nlargest(k, xs) | O(n log k) |
| Sorted search | bisect.bisect_left(a, x) | O(log n) |
| Sort | sorted(xs, key=…) | O(n log n) |
| Memoisation | @lru_cache(None) | per distinct call |
| Prefix sums | accumulate(xs) | O(n) |
| LRU cache | OrderedDict.move_to_end, popitem(last=False) | O(1) |