Cohort 4Register

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.

#PatternThe question sounds likeCore toolPg
P1Frequency count"how many of each", "most common error"Counter, dict.get(k, 0) + 159
P2Top-K"top 3 customers", "k largest files"heapq.nlargest, size-k min-heap59
P3Group by key"totals per order", "items per user"defaultdict(list)60
P4Dedupe, keep latest"remove duplicates", "latest record per id"dict keyed by id + timestamp test60
P5Sliding window"rolling 3-day sum", "moving average"deque, running total61
P6Merge sorted streams"merge two sorted logs", "combine feeds"heapq.merge, two pointers61
P7Flatten nested JSON"flatten this payload", "explode items"recursion, json_normalize62
P8Sessionize events"split into sessions", "30 min idle"sort + gap test63
P9Consecutive streaks"longest login streak", "consecutive days"set + start-of-run test63
P10Parse logs"count errors per job from this log"re named groups + dict64
P11Merge intervals"overlapping bookings", "busy periods"sort by start, extend last65
P12Hash join"join these two files without pandas"dict on the small side65
P13Chunked processing"the file is 50 GB", "batch inserts"generator + islice66
P14Retry with backoff"the API returns 429", "flaky warehouse"exponential delay + jitter66
P15Paginated extraction"pull all pages from this API"cursor loop + generator67
P16Memoize / LRU cache"avoid repeat lookups", "build an LRU"lru_cache, OrderedDict67
P17Validate and quarantine"reject bad rows", "data contract"per-row rules, two outputs68
P18Diff two snapshots"what changed since yesterday", "CDC"set algebra on keys68

One skeleton covers half the list

THE ONE-PASS DICT SKELETONANY PYTHON
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, format

Practice before you move on

  1. For each of P1 to P6, write the phrase from a question that tells you which pattern it is.
  2. Use the one pass dict skeleton to total the amounts per status in orders.csv.