Stage 1 of 3: Rules
Background. An e-commerce service checks every order against a set of rules before it goes to the warehouse: some items may not be shipped, an order's total has to be within a range, and more rules arrive over time. The checks live in an OrderValidator that rules can be added to and removed from.
Implement NoProhibitedItems, TotalWithin and OrderValidator in validator.py.
- An
Orderhasid,items(a tuple of item names) andtotal. - A rule is any object with a
nameand acheck(order)method that returnsNonewhen the order passes and a short reason (a non-empty string) when it does not. NoProhibitedItems(prohibited), namedno_prohibited_items, fails an order that contains any of the prohibited item names (exact names, any iterable of them).TotalWithin(low, high), namedtotal_within, fails an order whose total is belowlowor abovehigh; both bounds are allowed.add_rule(rule)registers a rule; a second rule with the same name raisesValueError.remove_rule(name)unregisters one; an unknown name raisesKeyError.rules()lists the names in the order they were added.validate(order)runs every rule and returnsResult(ok, failed):failedmaps the name of each rule that failed to its reason, andokisTrueexactly when nothing failed. Every rule runs, whatever the others say.
Example
v = OrderValidator()
v.add_rule(NoProhibitedItems({"knife", "lighter"}))
v.add_rule(TotalWithin(10, 500))
v.validate(Order("o1", ("book", "mug"), 42.0)) # Result(ok=True, failed={})
r = v.validate(Order("o2", ("knife",), 900.0))
r.ok # False
sorted(r.failed) # ["no_prohibited_items", "total_within"]
Done when Submit goes green.
Constraints
- Python 3.12+ standard library only
- Time limit: 10 seconds per test file
- Editable:
validator.py - Read-only:
test_helpers.pyand alltests/test_*.pyfiles
What comes after stage 1
The next stages open once the tests for the one before them pass. Each raises the bar, the way an interviewer does in the room.
- Stage 2: Prerequisites. Rules wait for other rules; what a failure upstream does to everything downstream.
- Stage 3: Retiring a rule. A rule in the middle of the graph goes away; the graph has to stay honest.
The files you get
- Yours to edit: validator.py.
- Read-only: test_helpers.py, tests/__init__.py.
- Tests you can read and run: tests/test_part1_rules.py. Hidden tests run on Submit.
The round: an agent in the editor
The assistant here works like the one in the interview IDE. It reads your files, runs code and proposes changes, and you accept or reject each one. It doesn't volunteer ideas, so what gets built is what you asked for. Along the way the interviewer asks about your decisions, and at the end the report says what you checked and what you accepted unread.
Frequently asked, briefly answered
Is Order Validator: Rules with Prerequisites free?
It is a Pro problem. Free plan includes one complete problem, plus the first stage of several more, so you can see how a round works before you pay.
Which company asks something like this?
TikTok. It is modelled on TikTok's round: First-round AI-assisted coding interview: your own AI tool on a shared screen, a small system built from a thin brief with follow-ups on its edge cases, and tests expected before time runs out.
Do I bring my own AI tool?
No. The assistant is in the editor, same as in the interview IDEs at Meta, Stripe and Amazon. What you choose at Begin is the format: an agent that proposes changes you accept or reject, an answer-only assistant that can't write code, or no assistant at all.
What does the interviewer ask?
3 questions along the way about your own code, and one at the end about the whole session. A weak answer gets one follow up. The answers go into the report, which ends with a hire or no hire verdict.
Start the problem
Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the Pro plan starts at the first stage of this problem.
More like it: every practice problem, how TikTok runs its AI round, the AI coding interview guide.