Skip to main content

← All cheatsheets · Track →

🎯Updated 2026-05

Python interview cheatsheet — the 8 patterns that actually show up

Pared down to what real interviews ask. 8 algorithmic patterns + 5 system-design lite questions + STAR shape. Not a 200-problem LeetCode plan.

Algorithmic patterns

Hash map counting

Anagrams, two-sum, longest substring without repeats.

from collections import Counter
Counter("anagram") == Counter("nagaram")  # True

Two pointers

Sorted-array merge, palindrome check, container-with-water.

l, r = 0, len(s) - 1
while l < r:
    if s[l] != s[r]: return False
    l += 1; r -= 1

Sliding window

Longest substring, max-sum k window, anagrams of P in S.

l = best = 0
seen = set()
for r, c in enumerate(s):
    while c in seen: seen.discard(s[l]); l += 1
    seen.add(c)
    best = max(best, r - l + 1)

Stack parsing

Valid parens, daily temperatures, evaluate RPN.

stack = []
for c in s:
    if c in "([{": stack.append(c)
    elif not stack or pair(stack.pop()) != c: return False
return not stack

BFS / DFS

Level-order, number-of-islands, course-schedule.

from collections import deque
q = deque([start]); seen = {start}
while q:
    node = q.popleft()
    for nb in graph[node]:
        if nb not in seen:
            seen.add(nb); q.append(nb)

Heap

Top-K, kth largest, merge K sorted lists.

import heapq
h = []
for x in nums:
    heapq.heappush(h, x)
    if len(h) > k: heapq.heappop(h)

Binary search on answer

Min in rotated array, capacity to ship in D days.

lo, hi = 1, max(weights)
while lo < hi:
    mid = (lo + hi) // 2
    if feasible(mid): hi = mid
    else: lo = mid + 1

Dynamic programming

Longest-common, knapsack, coin-change, edit distance.

# Bottom-up coin change
dp = [float("inf")] * (amount + 1); dp[0] = 0
for a in range(1, amount + 1):
    for c in coins:
        if c <= a: dp[a] = min(dp[a], dp[a - c] + 1)

Behavioural — STAR

The shape

Situation → Task → Action → Result. Numbers in Result.

# "Latency p95 was 480ms (S). Need <200ms before launch (T). Replaced the
# synchronous Stripe call with a Celery write-behind queue (A). Hit 92ms
# p95 in load tests, shipped on time (R)."

What to keep ready

Have 3 stories memorised — one each for: challenge, conflict, ownership.

# Each story 60–90s spoken. Stick to one project per story.

Anti-patterns

Skip these — they're tells.

# - "I worked hard"  → no Action specificity
# - "We did X"        → no individual ownership
# - "I learned a lot" → no Result
# - silence > 4s     → start narrating, even mid-thought

System design — junior cuts

URL shortener

Hash → DB lookup. Don't over-engineer.

# Snowflake-style ID → base62 slug. Cache slug→URL in Redis.
# Rate-limit POSTs per IP.

Rate limiter

Token bucket vs sliding window — be able to sketch both.

# Sliding window via Redis Lua: ZADD timestamps + ZREMRANGEBYSCORE + ZCARD.

Notification system

Async queue + retries + dedup.

# Celery worker per channel. Idempotency key per notification.
# DLQ for poison messages.

TODO API with offline sync

Conflict resolution — last-write-wins or CRDT?

# Last-write-wins: include `updated_at`. Server keeps latest.
# Mention CRDTs for "real" sync.

Pagination over fast feed

Cursor, not offset — page stable under inserts.

GET /feed?cursor=<created_at>&limit=20
# Response includes `next_cursor`.

Want to actually learn these patterns, not just paste them? Open the Python interview cheatsheet track — each snippet has a full lesson behind it.