Python, properly · Chapter 42 of 42
Capstone and interview round
By the end of this chapter you have built DabbaDrop's order service the way a team ships Python: validated by Pydantic, checked by mypy, proven by tests that catch real bugs, and packaged with uv as a command anyone can install. Then you solve two classic interview problems against the clock. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Real-world Python
- 5 tasks
- Python
- In the all labs plan
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.
Why you need this
Farah's message this morning: "The website team wants to quote and place orders from their own scripts. Can they just install yours?" Rafiq adds: "And can you prove the coupons are right? Last month's FIRST50 bug cost us money."
Every piece already exists somewhere in this lab: Pydantic (chapter 34), httpx (37), type hints and mypy (33), pytest (21), uv (20), argparse (41). What a real project needs is all of them together, in one module that other people can trust and install. And Kunal has a last surprise: the interview round he gives every new developer.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
order_service.py: the order service, typed
The website team wants DabbaDrop's order logic in one module. OrderRequest is a Pydantic model that refuses bad input with a ValidationError: customer_id is C and exactly three digits (C033), items is {dish_id: quantity} with at least one dish and every quantity a whole number of at least 1, and coupon is DABBA10, FIRST50 or None (the default).
load_prices() returns {dish_id: price} for every dish in /workspace/data/menu.json. quote(request, prices) returns the total: price times quantity for each dish, then DABBA10 takes 10% off as round(total * 0.9) and FIRST50 takes Rs 50 off, never below 0; a dish not in prices raises UnknownDish. place(request) POSTs the order to the API with the key, raises for an error status, and returns the new order as a dict.
The check imports them and tries each rule, places a real order, and runs mypy --strict on the file, which must find no problems.
- Task 2
test_order_service.py: tests that catch bugs
Rafiq wants proof that the coupons are right. Write pytest tests for quote and OrderRequest, using only OrderRequest, quote and UnknownDish from order_service, with a small prices dict of your own.
The check runs your tests on your own order_service.py and on another correct one (both must pass), then on six copies with one bug each, and each copy must make at least one test fail: DABBA10 ignored, FIRST50 ignored, FIRST50 taking a total below 0, quantities ignored, a dish not in prices skipped instead of raising UnknownDish, and a quantity of 0 accepted by OrderRequest.
- Task 3
pyproject.toml: packaged with uv
Make the service an installable command. First write main(argv=None) in order_service.py with argparse: a customer id, one or more dish-id:quantity items, --coupon (DABBA10 or FIRST50 only, any other is a usage error, exit 2) and a --place flag. It prints Total: Rs 130 for C033 roti:2 chole:1 --coupon FIRST50, or with --place sends the order and prints Placed N0007: Rs 130 (the new order's id), and returns 0. An item that is not dish:quantity, a request OrderRequest refuses, or an unknown dish prints a message on stderr and returns 1.
Then write pyproject.toml: the project dabba-orders with a version, requires-python >= 3.13 and the dependencies httpx and pydantic, the command dabba-orders under [project.scripts] pointing at order_service:main, and the build lines given in the file. The check lays it out like uv init --package (order_service.py becomes src/order_service/__init__.py), runs uv build, installs the wheel in a fresh virtual environment and runs dabba-orders.
- Task 4 · interview question
Interview: pair_for_budget.py, two dishes for the budget
Time yourself: 20 minutes. two_dishes(prices, budget) returns (i, j), the positions of two prices with i < j that add up to exactly budget, or None if there are none. If several pairs work, return the one whose j is smallest, and for that j the smallest i: two_dishes([140, 60, 90, 80], 150) returns (1, 2), two_dishes([75, 75], 150) returns (0, 1), two_dishes([75], 150) returns None.
The check compares you with a slow, sure answer on small lists, then times you on 200,000 prices: under 2 seconds.
- Task 5 · interview question
Interview: merge_shifts.py, merge the riders' shifts
Time yourself: 20 minutes. merge_shifts(shifts) takes riders' shifts as (start, end) minutes in any order and returns them merged: any two that overlap or touch (one ends the minute the other starts) become one, sorted by start, as a list of (start, end) tuples. merge_shifts([(600, 720), (690, 780), (900, 960)]) returns [(600, 780), (900, 960)]; merge_shifts([(600, 660), (660, 700)]) returns [(600, 700)]; a shift inside another disappears into it; [] gives [].
The check tries small cases against a sure answer, then times you on 200,000 shifts: under 3 seconds.
What you will learn
- One module, four layers: a model that refuses bad input, pure functions that compute, a thin layer that talks to the API, and a command line on top.
- mypy --strict: the type checker at its strictest, and the Any that r.json() leaks into your code.
- Tests that catch bugs: why passing tests are not enough, and mutation testing.
- Packaging with uv: [project.scripts], a build backend, a wheel, and installing it.
- Interview problems: thinking in big O, and measuring it.
More in Real-world Python
Build "Capstone and interview round" yourself
DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.