PART 5 · CODING ROUND PATTERNS
The pattern map: 18 patterns at a glance
FreeLesson 56 of 78
Most DE Python coding questions are one of these 18 shapes, or two of them stacked. Learn to hear the shape inside the question.
| # | Pattern | The question sounds like | Core tool | Pg |
|---|---|---|---|---|
| P1 | Frequency count | "how many of each", "most common error" | Counter, dict.get(k, 0) + 1 | 59 |
| P2 | Top-K | "top 3 customers", "k largest files" | heapq.nlargest, size-k min-heap | 59 |
| P3 | Group by key | "totals per order", "items per user" | defaultdict(list) | 60 |
| P4 | Dedupe, keep latest | "remove duplicates", "latest record per id" | dict keyed by id + timestamp test | 60 |
| P5 | Sliding window | "rolling 3-day sum", "moving average" | deque, running total | 61 |
| P6 | Merge sorted streams | "merge two sorted logs", "combine feeds" | heapq.merge, two pointers | 61 |
| P7 | Flatten nested JSON | "flatten this payload", "explode items" | recursion, json_normalize | 62 |
| P8 | Sessionize events | "split into sessions", "30 min idle" | sort + gap test | 63 |
| P9 | Consecutive streaks | "longest login streak", "consecutive days" | set + start-of-run test | 63 |
| P10 | Parse logs | "count errors per job from this log" | re named groups + dict | 64 |
| P11 | Merge intervals | "overlapping bookings", "busy periods" | sort by start, extend last | 65 |
| P12 | Hash join | "join these two files without pandas" | dict on the small side | 65 |
| P13 | Chunked processing | "the file is 50 GB", "batch inserts" | generator + islice | 66 |
| P14 | Retry with backoff | "the API returns 429", "flaky warehouse" | exponential delay + jitter | 66 |
| P15 | Paginated extraction | "pull all pages from this API" | cursor loop + generator | 67 |
| P16 | Memoize / LRU cache | "avoid repeat lookups", "build an LRU" | lru_cache, OrderedDict | 67 |
| P17 | Validate and quarantine | "reject bad rows", "data contract" | per-row rules, two outputs | 68 |
| P18 | Diff two snapshots | "what changed since yesterday", "CDC" | set algebra on keys | 68 |
One skeleton covers half the list
result = {} # or defaultdict(...) / Counter()
for record in stream: # one pass over the input: O(n)
key = key_of(record) # what defines "the same thing"?
result[key] = combine(result.get(key), record) # count, sum, append, keep latest
answer = finish(result) # sort, take top k, formatPractice before you move on
- For each of P1 to P6, write the phrase from a question that tells you which pattern it is.
- Use the one pass dict skeleton to total the amounts per status in orders.csv.