Stage 1 of 3: Who owes what
Background. Friends on a trip keep a list of what each of them paid for the group. Each expense records who paid, the total in cents, and the people it was for, who owe equal shares of it.
Implement balances(expenses) in expenses.py. It returns what each person is owed or owes once every expense is counted.
expensesis a list of(payer, amount, shared_by)tuples, possibly empty:payerpaidamountcents for the people listed inshared_by.amountis anintfrom 0 to 10^9, andshared_byis a list of names, never empty.- An expense is split into whole cents among the
k = len(shared_by)people listed: each owesamount // k, and the firstamount % kof them, in the ordershared_bylists them, owe one cent more. The shares add up toamountexactly. - The payer's balance rises by
amount, and the balance of each person listed falls by their share. The payer may be listed, at any position, and then owes a share like everyone listed; a payer who is not listed owes nothing for that expense. A name listed twice owes a share for each listing, each at its own position. - Return a dict from every person named in any expense, as payer or in
shared_by, to their balance, anint: positive when the others owe them, negative when they owe,0when they are even. Nobody else has an entry, andbalances([])is{}.
Example
balances([
("Ravi", 1000, ["Ravi", "Sofia", "Tunde"]),
("Sofia", 450, ["Tunde"]),
])
# {"Ravi": 666, "Sofia": 117, "Tunde": -783}
The 1000 cents split three ways are 334, 333 and 333, and Ravi, listed first, owes the extra cent: he is owed 1000 - 334 = 666.
Notes
- Names are case-sensitive:
"Ana"and"ana"are two people. - An amount of 0 changes no balance, but its payer and the people listed still get entries.
- No amount is negative and no
shared_byis empty; nothing else needs checking. - On the starter, all 11 tests in
tests/test_part1.pyfail.
Done when Submit goes green.
Constraints
- Python 3.12 standard library only
- Editable:
expenses.py - Read-only: all
tests/test_*.pyfiles - Amounts are whole cents, an
intfrom 0 to 10^9
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: Settle up. Turn the balances into transfers that leave everyone even, in a short list.
- Stage 3: Fewest transfers. The same plan, but no other plan for the group may use fewer transfers.
The files you get
- Yours to edit: expenses.py.
- Tests you can read and run: tests/test_part1.py. Hidden tests run on Submit.
The round: no AI, an assistant that only answers
Pinterest runs this round without AI. Here the closest format is answer-only: the assistant answers your questions and reviews your code when you ask, the way an interviewer would, and never writes a line of it. The interviewer asks about your decisions as you go, and the report states the format. You can switch to the agent format when you press Begin.
Frequently asked, briefly answered
Is Debt Settlement 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?
Pinterest. It is modelled on Pinterest's coding round, which runs without AI. Based on candidates' reports of Pinterest interviews. Nyrion wrote this version, so the question you get may differ.
Do I bring my own AI tool?
No. The real round has no AI, so this one opens answer-only: an assistant that answers your questions and can't write code. You can switch to an agent that proposes changes when you press Begin.
What does the interviewer ask?
6 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, Pinterest's interviews, the AI coding interview guide.