Joel Stevick

The Python readiness check · 75 minutes · 2 tasks

Senior Python coding interview practice test

Developers applying for a senior Python role, whose screen asks for an algorithm under pressure and a small design with state and a tricky specification.

A new test every time, written by Claude, like the screens on HackerRank, CodeSignal and Codility: 75 minutes, 2 tasks, hidden tests that score it and a review of your code. Every senior test is paid: $5, or $19 for five.

The topics

Graphs, Heaps, Dynamic programming, Intervals, Designing with state, Concurrency, Reading a tricky specification, The collections module.

A sample task: The cheapest route with at most k stops

A task from this level, whole, as the test shows it. Your test is a new one, written for you.

There are n cities numbered 0 to n - 1 and a list of one-way flights, each (origin, destination, price) with a price of 0 or more. Write cheapest(n, flights, src, dst, k), which returns the lowest total price of a trip from src to dst that makes at most k stops in between (so at most k + 1 flights), or -1 if there is no such trip. A trip from a city to itself costs 0. There may be several flights between the same two cities, and cycles.

The tests include graphs of a few thousand flights; an answer that tries every path will run out of time.

flights = [(0, 1, 100), (1, 2, 100), (0, 2, 500)]
cheapest(3, flights, 0, 2, 1)  # 200: 0 -> 1 -> 2, one stop
cheapest(3, flights, 0, 2, 0)  # 500: no stops allowed

Starter code

def cheapest(n, flights, src, dst, k):
    # Return the lowest price from src to dst with at most k stops, or -1.
    return -1

Example tests

FLIGHTS = [(0, 1, 100), (1, 2, 100), (0, 2, 500)]


def test_example_one_stop():
    assert cheapest(3, FLIGHTS, 0, 2, 1) == 200


def test_example_no_stops():
    assert cheapest(3, FLIGHTS, 0, 2, 0) == 500

Hidden tests (13 of them for this task) check the edge cases the statement names. You never see them, as on a real screen.

A sample report

A real graded submission with mistakes: its test results come from the harness run by a local Python 3.9.6 (npm run assessment:anchors:local) on 2026-11-22; its review was written by hand as the sample review, for a submission whose test results come from a real run of the harness; not a live model run.

Close to ready for a senior Python screen

15 of 24 hidden tests passed. Ready means at most one hidden test failed on every task; close means at least 60% of them passed overall.

The route search is nearly right and fails one case that tests understanding of the stop limit; the rate limiter misreads the specification in three ways. Close overall, but the second task alone would not pass a senior screen.

Task 1: The cheapest route with at most k stops

12 of 13 hidden tests passed.

  • test_cheap_way_to_a_stop_uses_too_many_stops: AssertionError

Summary. A Dijkstra search with the stop count in the heap is a reasonable start, and it passes twelve of thirteen tests, including the large graph. Pruning by cost alone throws away paths that are dearer but use fewer stops.

What failed and why, most important first:

  1. test_cheap_way_to_a_stop_uses_too_many_stops: best[city] keeps only the cheapest cost seen, whatever its number of stops. When the cheap way to city 3 uses three stops, the dearer one-stop way is never pushed, and then the cheap entry is dropped for having too many stops, so you return -1 instead of 7. Keep the best cost per (city, stops), or relax all edges k + 1 times (Bellman-Ford limited to k + 1 rounds).

Edge cases. Unreachable cities, one-way flights, cycles, parallel flights and free flights are handled.

Complexity. O(E log E) when it is right, which is fine. The bounded Bellman-Ford is O(k * E) and simpler to prove correct with a stop limit; say which you chose and why.

Readability. Clear. The tuple (cost, city, stops) is easy to follow; a comment on why the dst check comes before the stops check would help a reviewer.

Your code
import heapq


def cheapest(n, flights, src, dst, k):
    graph = {}
    for a, b, price in flights:
        graph.setdefault(a, []).append((b, price))
    best = {src: 0}
    queue = [(0, src, 0)]
    while queue:
        cost, city, stops = heapq.heappop(queue)
        if city == dst:
            return cost
        if stops > k:
            continue
        for nxt, price in graph.get(city, []):
            if cost + price < best.get(nxt, float("inf")):
                best[nxt] = cost + price
                heapq.heappush(queue, (cost + price, nxt, stops + 1))
    return -1
A reference solution
def cheapest(n, flights, src, dst, k):
    if src == dst:
        return 0
    inf = float("inf")
    best = [inf] * n
    best[src] = 0
    for _ in range(k + 1):
        nxt = best[:]
        for origin, destination, price in flights:
            if best[origin] + price < nxt[destination]:
                nxt[destination] = best[origin] + price
        best = nxt
    return -1 if best[dst] == inf else best[dst]

Task 2: A sliding-window rate limiter

3 of 11 hidden tests passed.

  • test_zero_limit_raises: AssertionError: limit 0 did not raise ValueError
  • test_zero_window_raises: AssertionError: window 0 did not raise ValueError
  • test_refused_requests_do_not_count: AssertionError
  • test_window_is_half_open: AssertionError
  • test_time_going_backwards_raises: AssertionError: an earlier now did not raise ValueError
  • test_old_requests_expire_one_by_one: AssertionError
  • test_float_times: AssertionError
  • test_many_requests_stay_fast_and_bounded: Slow: took longer than 2 seconds

Summary. The per-key state and the window filter are the right idea, but the code records refused requests, never validates its arguments or the clock, and rebuilds the list on every call.

What failed and why, most important first:

  1. test_refused_requests_do_not_count: You append now before deciding, so a refused request is recorded and pushes out later ones. Decide first, and append only when you return True.
  2. test_time_going_backwards_raises: The statement asks for ValueError when now is earlier than the previous call's now, for any key. Keep the last now on the limiter, not per key.
  3. test_zero_limit_raises: limit or window of 0 or less must raise ValueError in the constructor (test_zero_window_raises is the same).
  4. test_many_requests_stay_fast_and_bounded: The list comprehension rebuilds the whole list on every call, which is O(limit) per request plus an allocation, and here it took over two seconds. A deque per key, popping expired times from the left, is amortized O(1).

Edge cases. Independent keys work. The half-open window and same-instant requests only fail because refused requests were recorded.

Complexity. O(n) per call in the size of the window's list; the expected answer is amortized constant time with bounded memory.

Readability. Short, but the order of operations (record, then decide) hides the main bug. Writing the rule from the statement as a comment above the decision makes such slips easier to see.

Your code
class SlidingWindowLimiter:
    def __init__(self, limit, window):
        self.limit = limit
        self.window = window
        self.requests = {}

    def allow(self, key, now):
        times = self.requests.setdefault(key, [])
        times.append(now)
        recent = [t for t in times if t > now - self.window]
        self.requests[key] = recent
        return len(recent) <= self.limit
A reference solution
from collections import deque


class SlidingWindowLimiter:
    def __init__(self, limit, window):
        if limit <= 0 or window <= 0:
            raise ValueError("limit and window must be positive")
        self.limit = limit
        self.window = window
        self.allowed = {}
        self.last = None

    def allow(self, key, now):
        if self.last is not None and now < self.last:
            raise ValueError("time went backwards")
        self.last = now
        times = self.allowed.setdefault(key, deque())
        while times and times[0] <= now - self.window:
            times.popleft()
        if len(times) >= self.limit:
            return False
        times.append(now)
        return True

A proctored screen would have flagged 0 pastes into the editor and 1 time the tab was left. Nothing was blocked here; on a real screen these are often reviewed by a person.

What to practise

  • Reading a tricky specification: practise with 17-first-test in the Python tutorial.
  • Designing with state: practise with 09-dataclasses in the Python tutorial.
  • Graphs: no exercise in the tutorial drills this yet.

Start a test

All four levels and the questions people ask: the Python readiness check.