Mon 24 August 2026
Python Under Pressure
The first software book I bought was Effective Python by
Brett Slatkin. The book distills tips and tricks learnt
by Brett over the course of his career. It was in this book
I first encountered how to use the enumerate keyword. Over
the course of my career a new hint like this one has sparked
joy and I've experienced first hand how pulling one of these
out of the back pocket during a live interview can intrigue
an interviewer.
One can speak a language their whole life and every so often come across a new word that you've not encountered before but it does a very good job of describing the current situation. The same happen in software.
Python is a language with a large standard library that
offers many tools, so much so that you can even import antigravity.
Here's a shortlist of some of the tricks you can do when you
have to write python under pressure.
Modular Operator
Running into a situation where you need to cycle through
indexes in an array the modular operator % is the tool of
choice.
Using it we can have x cycle from 0 -> 5 and back to 0. It's application extends to explaining how hash look ups work in a key value store and providing an answer to a simple question like how much remains after dividing 5 by 3.
# x cycles from 0 -> 5 and back to 0.
x = 0
x = (x+1) % 6
# How much remains after dividing 5 by 3
>>> 5 % 3
2
Floor division
When we don't care about the remainder we can use a floor division to provide a nice round int. This answers the simple question, how many times can 4 fit into 11:
>>> 11 // 4
2
Divmod
When we can't remember if we should be using % or // in
the middle of a live interview then we can use the builtin
keyword divmod to give us both answers.
>>> divmod(6, 4)
(1, 2)
dict.setdefault
There are some tricky problems that want you to set a key if
it doesn't exist in a map but if it already exists then
avoid updating the map but ensure the value being set is the
same as the value that has already been set. Obviously you
can do this without setdefault.
x = {"foo": 4}
new_value = 10
old_value = x.get("foo")
if not old_value:
x["foo"] = new_value
else:
if old_value == new_value:
raise
Here's how you can do it with setdefault.
x = {"foo": 4}
new_value = 10
old_value = x.setdefault("foo", new_value)
if old_value == new_value:
raise
Setdefault will insert the key with the new value if the key isn't already in the dictionary. When it is already in the dictionary it returns that value otherwise it sets the provided value and also returns that value.
Greatest common denominator
If you need to perfectly tile a 21x35 rectangle with squares, what is the size of largest square that will cover the area of this rectangle?
The answer is the largest number that the two numbers, 21 and 35, can be divided by.
>>> import math
>>> math.gcd(21, 35)
7
Thus the largest size square is 7x7.
Prefix sum
Prefix sums is a common technique used to solve coding problems such as "Subarray Sum Equals k". A prefix sum at index i represents the sum of all items from 0 to i. We can create a prefix sum in python using itertools.
>>> import itertools
>>> list(itertools.accumulate([1, 3, 4, 3, 2]))
[1, 4, 8, 11, 13]
Defaultdict
Defaultdict is a classic tool which allows you to specify
the default instantiation for a key. If you need to track a
list of elements for specific keys you can instantiate the
default dict with list. To avoid checking if a key
already exists and creating a new list if not.
>>> from collections import defaultdict
>>> tracking = defaultdict(list)
>>> tracking["x"].append(1)
>>> tracking
{"x": [1]}
It also plays nicely with counting.
>>> from collections import defaultdict
>>> tracking = defaultdict(int)
>>> tracking["x"] += 1
>>> tracking
{"x": 1}
Heaps
Tracking key usage or needing a priority ordered queue will require using a min/max heap. Fortunately python offers methods that transforms lists into these heaps.
from heapq import heapify
queue = [3, 1, 2, 4]
heapify(queue)
Relying on heappush and heappop allow us to dequeue or
enqueue items to our heap while maintaining priority order.
deque vs list
I've covered deque before in essence we can't always rely on the builtin list as they are dynamic arrays, we need to remove from the front and pop from the back in constant time. The deque is a builtin solution for linked lists.
from queue import deque
q = deque()
q.append(1)
q.appendleft(2)
q.pop()
1
Bisect
Another one already covered.
This is Python's own implementation of binary search. If we
are given a sorted array and wish to insert a new item while
maintaining order we can use bisect_left.
from bisect import bisect_left
items = [1, 2, 4, 5, 5, 6]
bisect_left(items, 5)
3