Pro track · 45 minutes
Forty-five minutes: parse, join, write once
Parsing, a join to the three orders, an idempotent write, and tests. No puzzles. No trick input.
Pro practice beside the interview page. It does not replace the six weeks. These checks are not part of the 31 in python3 test_all.py. Python 3.11 or newer. Orders stay 1042 shipped, 1043 paid, 1044 cancelled.
Set a timer for 45 minutes. Work in the lab/ folder from lab.zip. Use northline.orders. Do not add a send tool. Do not print emails.
The line you parse
One shape only. Anything else is parse_failed. You do not guess a nearby order id.
order 1042 refund 1000 cents key k1 order 1043 refund 1500 cents key k2 order 1044 refund 100 cents key k3
import re
from northline.orders import ORDERS, refund_order
LINE = re.compile(
r"^order (?P<order_id>1042|1043|1044) refund (?P<amount_cents>[0-9]+) cents key (?P<idempotency_key>[a-z0-9-]+)$"
)
def parse_line(line: str) -> dict:
m = LINE.fullmatch(line.strip())
if not m:
return {"error": "parse_failed"}
out = m.groupdict()
out["amount_cents"] = int(out["amount_cents"])
return out
def join_order(parsed: dict) -> dict:
if "error" in parsed:
return parsed
row = ORDERS.get(parsed["order_id"])
if row is None:
return {"error": "not_found", "order_id": parsed["order_id"]}
joined = dict(parsed)
joined["state"] = row["state"]
joined["total_cents"] = row["total_cents"]
return joined
def write_refund(joined: dict, ledger: list, *, confirmed: bool) -> dict:
if "error" in joined:
return joined
return refund_order(
{
"order_id": joined["order_id"],
"amount_cents": joined["amount_cents"],
"idempotency_key": joined["idempotency_key"],
},
confirmed=confirmed,
ledger=ledger,
)
confirmed is an argument your test sets. It is not the word "yes" inside the line. The line has no email field. The join must not copy email out of ORDERS.
Tests to write
Four tests. Each one has an obvious expected result. If a test fails, the behavior moved. There is no hidden case.
order 9999 refund 100 cents key areturnsparse_failed. 9999 is not one of the three ids. Do not look it up.order 1044 refund 100 cents key k3joins to statecancelled.write_refund(..., confirmed=True)returnsstate_blocked. The ledger stays empty.order 1042 refund 1000 cents key k1withconfirmed=Truecommits once. The same call again returnsalready_committed. The ledger length is 1.confirmed=Falseon that same 1042 line returnsnot_confirmed. The ledger stays empty. The joined dict has noemailkey.
ledger = []
assert parse_line("order 9999 refund 100 cents key a")["error"] == "parse_failed"
cancelled = join_order(parse_line("order 1044 refund 100 cents key k3"))
assert cancelled["state"] == "cancelled"
assert write_refund(cancelled, ledger, confirmed=True)["error"] == "state_blocked"
assert ledger == []
line = parse_line("order 1042 refund 1000 cents key k1")
joined = join_order(line)
assert "email" not in joined
assert write_refund(joined, ledger, confirmed=False)["error"] == "not_confirmed"
assert ledger == []
assert write_refund(joined, ledger, confirmed=True)["status"] == "committed"
assert write_refund(joined, ledger, confirmed=True)["status"] == "already_committed"
assert len(ledger) == 1
Save the functions and the asserts as one file inside lab/, next to test_all.py, and run that file with Python 3.11 or newer. Then run python3 test_all.py and expect the same 31 passes. You did not add these asserts to that file. On Windows the suite command is py test_all.py.
What you say after the timer
Open the interview page. In one minute: you parsed a fixed line, joined it to the fictional order row without the email, and the write was idempotent because the ledger keeps the key. Confirmation did not come from the text. You studied this. You were not placed.