Skip to the workspace
Pebellslearn by building
Thinking in Patterns  ›  Lesson

Python as Your Interview Language

Python 14 minInterviews reward fluency in one language, and in Python that means knowing sets, dictionaries, Counter, defaultdict, deque, heapq and sorted with a key, along with what each operation costs.

leetcode / Python as Your Interview Language

Explore each scene at your own pace.

01 · The rule

The rule

Python as Your Interview Language

Most interviews let you choose the language. Python, JavaScript, Java, C++, Go, Rust, Ruby and C are all accepted almost everywhere, and the right choice is simply the one you write fastest without looking things up. This course uses Python: it is short to write, easy to read aloud, and its standard library already holds most of the tools the patterns need.

The rule

Pick one language and know its toolbox cold, costs included.

"Cold" means you never pause to remember a name, and "costs included" means you know how long each tool takes. These are the pieces that turn the patterns in this course into a few lines:

from collections import Counter, defaultdict, deque
import heapq

seen = set()                     # membership test is instant on average
counts = Counter("banana")       # Counter({'a': 3, 'n': 2, 'b': 1})
groups = defaultdict(list)       # a missing key starts as an empty list
queue = deque([1, 2, 3])         # fast at both ends: popleft(), appendleft()
heap = [7, 2, 9, 4]
heapq.heapify(heap)              # heap[0] is now the smallest item
smallest = heapq.heappop(heap)   # 2
by_length = sorted(["kiwi", "fig", "banana"], key=len)   # ['fig', 'kiwi', 'banana']

A few facts worth knowing by heart:

  • x in some_set and key in some_dict take roughly constant time. x in some_list scans the whole list.
  • some_list.pop(0) shifts every remaining item, so use deque.popleft() for queues.
  • sorted takes a key. A tuple key breaks ties: key=lambda t: (-t[1], t[0]) sorts by the second field descending, then by the first ascending.
  • Python integers never overflow, and // rounds toward minus infinity, so -7 // 2 is -4.
  • Strings cannot be changed in place. Build a list of pieces and "".join them at the end.
  • Recursion stops at a depth of about 1,000 by default, which matters for very deep recursive solutions.

If you choose another language, build the same list for it before your first interview: its hash map and set, its double-ended queue, its heap, and its sort with a custom order.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.