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_setandkey in some_dicttake roughly constant time.x in some_listscans the whole list.some_list.pop(0)shifts every remaining item, so usedeque.popleft()for queues.sortedtakes akey. 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 // 2is-4. - Strings cannot be changed in place. Build a list of pieces and
"".jointhem 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.