bigonotes
DSA: Patterns & PythonHacks & Cheat Sheets

Python Hacks for Coding Interviews

Standard-library tools and one-liners that make interview Python shorter, faster and less buggy, with the internals behind them.

6 min readLast updated

In an interview, every minute spent hand-writing a frequency loop or debugging an aliased grid is a minute not spent on the problem itself. This cheat sheet collects the Python idioms and standard-library tools that save that time. Each section is short and stands on its own, and each one ties back to the internals that make the idiom correct and fast.

Integers and bits#

Python ints never overflow, so you can compute 2**100 directly and skip max-int overflow checks. That also makes bit tricks safe to write, as long as the numbers aren't negative. These are the ones you'll use most:

x = 5
print(bin(x))            # '0b101'
print((x >> 2) & 1)      # 1: is bit 2 set?
print(x & (x - 1))       # 4: clears the lowest set bit
print(x ^ x, x ^ 0)      # 0 5: a^a = 0 and a^0 = a ("Single Number")
print(int("101", 2))     # 5: parse binary without a loop

mask = 0
mask |= 1 << 3           # bitmask as a compact visited-set
print(mask >> 3 & 1)     # 1

Strings#

Strings are immutable, so += in a loop copies everything each time (O(n2)O(n^2) total). Collect pieces in a list and join once. The snippet also shows reversing and the ord/chr pair for mapping letters to indexes:

s = "hello"
parts = []
for ch in s:
    parts.append(ch.upper())   # amortized O(1)
print("".join(parts))          # 'HELLO'

print(s[::-1])                 # 'olleh': reversed copy
print(ord("b") - ord("a"))     # 1: char -> 0..25 index
print(chr(97))                 # 'a'

Counting and grouping#

Counter and defaultdict replace most hand-written counting and grouping loops. The example ends with the Group Anagrams idea: a sorted word as the group key.

from collections import Counter, defaultdict

counts = Counter("banana")
print(counts)                  # Counter({'a': 3, 'n': 2, 'b': 1})
print(counts.most_common(2))   # [('a', 3), ('n', 2)]
print(Counter("abc") == Counter("cab"))   # True: anagram check

groups = defaultdict(list)     # missing keys start as []
for word in ["eat", "tea", "tan"]:
    groups["".join(sorted(word))].append(word)
print(dict(groups))
# {'aet': ['eat', 'tea'], 'ant': ['tan']}

Sorting with tuple keys#

Python's sort is stable and compares tuples element by element, so one key= handles multi-criteria ordering. Negate a number to flip it to descending:

data = [("Alice", 25), ("Bob", 20), ("Charlie", 25)]
data.sort(key=lambda x: (-x[1], x[0]))   # score desc, then name asc
print(data)   # [('Alice', 25), ('Charlie', 25), ('Bob', 20)]

(-25, "Alice") and (-25, "Charlie") tie on the first element, so the name breaks the tie.

Comprehensions and grids#

Comprehensions are shorter and usually faster than an equivalent for loop with append. Grid problems lean on them most, so the snippet includes the safe way to build a grid plus flatten, transpose and rotate:

nums = [1, 2, 3, 4, 5, 6]
evens = [x for x in nums if x % 2 == 0]

rows, cols = 3, 4
grid = [[0] * cols for _ in range(rows)]   # independent rows
# NOT [[0] * cols] * rows: that repeats ONE row object

matrix = [[1, 2], [3, 4]]
flat = [v for row in matrix for v in row]        # [1, 2, 3, 4]
transposed = [list(r) for r in zip(*matrix)]     # [[1, 3], [2, 4]]
# rotate 90° clockwise: reverse the rows, then transpose
rotated = [list(r) for r in zip(*matrix[::-1])]  # [[3, 1], [4, 2]]
print(flat, transposed, rotated)

Heaps, deques and ordered dicts#

These three containers cover priority queues, BFS queues and LRU caches. Each one replaces a structure you'd otherwise build by hand.

import heapq
from collections import deque, OrderedDict

nums = [5, 1, 8, 3]
heapq.heapify(nums)                 # O(n), min-heap in place
heapq.heappush(nums, 2)             # O(log n)
print(heapq.heappop(nums))          # 1: smallest first

# only a min-heap exists: negate for max
max_heap = [-x for x in [5, 1, 8]]
heapq.heapify(max_heap)
print(-max_heap[0])                 # 8

print(heapq.nlargest(2, [5, 1, 8, 3]))   # [8, 5]

q = deque([1, 2, 3])                # takes an iterable
q.append(4); print(q.popleft())     # 1: O(1) at both ends (BFS queue)

lru = OrderedDict(a=1, b=2, c=3)
lru.move_to_end("a")                # mark as most recently used
print(lru.popitem(last=False))      # ('b', 2): evict the oldest

Binary search with bisect#

bisect gives you a correct binary search on a sorted list without writing the loop yourself. The difference between bisect_left and bisect_right matters when the value appears more than once:

import bisect

a = [1, 3, 3, 5]
print(bisect.bisect_left(a, 3))   # 1: first index where 3 could go
print(bisect.bisect_right(a, 3))  # 3: index after the last 3
bisect.insort(a, 4)               # insert keeping order (O(n) shift)
print(a)                          # [1, 3, 3, 4, 5]

Memoization#

lru_cache turns a plain recursive function into top-down DP with one line. Deep recursion can hit Python's recursion limit, so the snippet raises it first.

from functools import lru_cache
import sys

# the default (~1000) is low for deep DFS/DP
sys.setrecursionlimit(10_000)

@lru_cache(maxsize=None)           # or functools.cache (3.9+)
def fib(n):
    return n if n < 2 else fib(n - 1) + fib(n - 2)

print(fib(80))                     # 23416728348467685

The cache stores results in a dict keyed by the arguments, so arguments must be hashable. Pass tuples, not lists.

Quickselect for "k smallest / largest / closest"#

Sorting is the easy answer to "find the k smallest", but it does more work than you need. When you only need the top k (unordered), partition instead of sorting. Quicksort's partition step puts the pivot at its final index with smaller items on the left, so you recurse into one side only: O(n)O(n) on average versus O(nlog⁡n)O(n \log n) for a full sort.

import random

def k_smallest(nums, k):
    nums = nums[:]
    lo, hi = 0, len(nums) - 1
    while lo < hi:
        r = random.randint(lo, hi)
        nums[r], nums[hi] = nums[hi], nums[r]
        pivot, i = nums[hi], lo
        for j in range(lo, hi):           # Lomuto partition
            if nums[j] < pivot:
                nums[i], nums[j] = nums[j], nums[i]
                i += 1
        nums[i], nums[hi] = nums[hi], nums[i]
        if i == k - 1:
            break
        if i < k - 1:
            lo = i + 1        # answer boundary is to the right
        else:
            hi = i - 1        # ... or to the left
    return nums[:k]

print(sorted(k_smallest([7, 2, 9, 4, 1, 8], 3)))   # [1, 2, 4]

Use it for Kth Largest Element, K Closest Points to Origin and Top K Frequent Elements (partition on frequency).

Small snippets worth memorizing#

These short helpers come up often enough that it pays to know them by heart. Union-Find answers "are these two connected?" (the Union-Find note has the full version), and the box formula comes from Sudoku-style grids.

# Union-Find with path compression
parent = list(range(5))

def find(x):
    if parent[x] != x:
        parent[x] = find(parent[x])
    return parent[x]

def union(x, y):
    rx, ry = find(x), find(y)
    if rx == ry:
        return False  # already connected: this edge makes a cycle
    parent[rx] = ry
    return True

print(union(0, 1), union(1, 0))   # True False

# Sudoku: which 3x3 box is (row, col) in?
row, col = 4, 7
print((row // 3) * 3 + col // 3)  # 5

Quick reference#

Here's everything above in one table, with the slower or buggier habit each idiom replaces.

Goal Do this Avoid
Build a string "".join(parts) s += c in a loop
Count items Counter(items) manual if k in d loops
Group items defaultdict(list) checking for missing keys by hand
Multi-key sort key=lambda x: (k1, -k2) writing your own sort
Init a grid [[0] * n for _ in range(m)] [[0] * n] * m
Index + value enumerate(a) range(len(a))
Walk two lists zip(a, b) manual index bookkeeping
Max-heap heapq with negated values sorting after every insert
Queue deque.popleft() list.pop(0) (O(n)O(n))
Infinity sentinel float("inf") magic numbers like 999999
Memoize recursion @lru_cache(maxsize=None) a global dict plus boilerplate
Save a backtracking path res.append(path[:]) res.append(path) (same list every time)
Top k unordered quickselect or a size-k heap full sort

Key takeaways#

  • Lean on the standard library: Counter, defaultdict, deque, heapq, bisect, lru_cache.
  • Join strings once; never += in a loop.
  • Build grids with a comprehension so rows are independent.
  • heapq is a min-heap; negate values for a max-heap.
  • For "top k" questions, quickselect gives O(n)O(n) average without a full sort.
Revision notes · 2 min

Core idea#

Lean on the standard library. Counter, defaultdict, deque, heapq, bisect and lru_cache replace hand-written loops and remove common bugs.

Key points#

  • Ints never overflow. x & (x - 1) clears the lowest set bit; a ^ a == 0 solves Single Number.
  • A bitmask is a compact visited set: mask |= 1 << i.
  • Counter(a) == Counter(b) is an anagram check. most_common(k) gives the top counts.
  • heapq is a min-heap. Negate values for a max-heap. heapify is O(n)O(n).
  • OrderedDict.move_to_end and popitem(last=False) give an LRU cache.
  • bisect_left is the first index where x could go. bisect_right is the index after the last x.
  • @lru_cache(maxsize=None) turns recursion into top-down DP. Arguments must be hashable.
  • Raise sys.setrecursionlimit for deep DFS. The default is about 1000.
  • Rotate a matrix 90° clockwise: zip(*matrix[::-1]).
  • Sudoku box index: (row // 3) * 3 + col // 3.

Do this, not that#

Goal Do this Avoid
Build a string "".join(parts) s += c in a loop
Count or group Counter, defaultdict(list) manual missing-key checks
Multi-key sort key=lambda x: (k1, -k2) writing your own sort
Init a grid [[0] * n for _ in range(m)] [[0] * n] * m
Index + value enumerate(a), zip(a, b) range(len(a))
Queue deque.popleft() list.pop(0)
Infinity sentinel float("inf") 999999
Save a path res.append(path[:]) res.append(path)
Top k unordered quickselect or a size-k heap full sort

Pitfalls#

  • bisect.insort keeps order but shifts in O(n)O(n).
  • [[0] * n] * m repeats one row object.
  • Passing a list to an lru_cache function raises an error. Pass a tuple.
  • Union-Find union returning False means the edge makes a cycle.

Say it in an interview#

  • "For top k unordered, quickselect is O(n) on average instead of a full sort."
  • "Python only has a min-heap, so I push negated values for a max-heap."

Found a mistake? Email ask@bigonotes.com