Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

For every line inside a loop, name the operation, then name its cost, and multiply by the number of times the loop runs. Most "correct but too slow" submissions hide one expensive line inside a cheap-looking loop.

Finding the hidden slow lineStep 1 of 5
  1. The code

    shared = [x for x in a if x in b], where a and b are lists of up to 100,000 names each.

The trap is judging speed by how short the code looks. One in on the wrong container can turn a one-liner into the slowest line in the program.

Drills

Answer fast — trust the rule.

quick drill

Which of these takes roughly constant time on average?

quick drill

What does Counter("banana").most_common(1) return?

quick drill

After h = [7, 2, 9, 4] and heapq.heapify(h), what does heapq.heappop(h) return?

quick drill

Why reach for deque instead of a list when you remove items from the front again and again?

quick drill

What comes first in sorted([("ana", 3), ("ben", 5), ("cy", 5)], key=lambda t: (-t[1], t[0]))?

Remember

  • Pick one language and know its toolbox cold, costs included.
  • Shortcut: for each line in a loop, name its cost and multiply.
  • Trap: x in some_list inside a loop; make it a set first.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy does it time out?

A solution walks through 50,000 page visits and, for each one, runs if page not in visited: visited.append(page), where visited is a list. It gives the right answer but times out. Explain why, estimate the cost before and after your fix, and say what you would change.