Stage 1 of 2: Fill the orders
Background. A brokerage's customers can trade fractions of a share. Its trading desk fills those orders from a small inventory it keeps of each stock, but the market it trades with deals only in whole shares. After every order, the desk's inventory of that stock must be at least 0.00 and under 1.00 share.
Implement fill_orders(holdings, orders) in desk.py. Fill the orders in the order they are listed, each before the next, and return the desk's final inventory.
- Every amount is a string with exactly two decimals, such as
"0.07"or"12.50": shares in hundredths of a share, prices and money in dollars and cents. Every result is the exact value these rules give. holdingsis the starting inventory, a list of(symbol, shares)pairs, each symbol once, each amount from"0.00"to"0.99". A symbol not in it starts at 0.00.- Each order is a dict with
"symbol","side","price"(one share's current price) and exactly one of"shares"or"dollars"."side"is what the customer does: a"buy"takes the customer's shares out of the desk's inventory, a"sell"puts them in. - A
"shares"order is for that many shares. A"dollars"order is for the most hundredths of a share the money pays for at the order's price, rounded down, on either side:"2.00"dollars at"3.00"is 0.66 shares,"0.29"dollars at"1.00"is 0.29. An order that comes to 0.00 shares changes no inventory and trades nothing, but its symbol still counts as named by an order. - An order touches the inventory of its symbol and no other. To keep that inventory in range, the desk trades whole shares of the symbol with the market on each order: it buys when the order would take the inventory below 0.00 and sells when the order would leave it at 1.00 or more, as many shares as put it between 0.00 and 0.99. An order that leaves the inventory in that range trades nothing.
- Customers' own holdings are not tracked: every order fills in full.
- Return one
(symbol, shares)tuple per symbol named inholdingsor in any order, each once, with its final inventory,"0.00"included: first the symbols ofholdingsin their given order, then the others in the order of their first orders. Each call starts from its ownholdings.
Example
holdings = [("KITE", "0.20"), ("ORBX", "0.55")]
orders = [
{"symbol": "KITE", "side": "buy", "price": "12.00", "shares": "1.50"},
{"symbol": "KITE", "side": "sell", "price": "12.00", "dollars": "5.00"},
{"symbol": "ZETA", "side": "sell", "price": "3.00", "shares": "0.60"},
{"symbol": "KITE", "side": "buy", "price": "12.00", "shares": "0.11"},
]
fill_orders(holdings, orders) # [("KITE", "0.00"), ("ORBX", "0.55"), ("ZETA", "0.60")]
The first buy is 1.30 more than the 0.20 KITE held, so the desk buys 2 shares and keeps 0.70. The 5.00 dollar sale is 0.41 shares (0.42 would cost 5.04), which makes 1.11: the desk sells 1 share and keeps 0.11. The last buy takes exactly the 0.11 held, with no trade.
Notes
- Prices are from
"1.00"to"5000.00","shares"from"0.01"to"5000.00","dollars"from"0.01"to"50000.00". A call has at most 1,000 orders and 100 symbols; symbols are capital letters. Inputs are always valid, and what happens on invalid input is not specified. - On the starter, all 14 tests in
tests/test_part1.pyfail.
Done when Submit goes green.
Constraints
- Python 3.12 standard library only
- Editable:
desk.py - Read-only: all
tests/test_*.pyfiles - Amounts are strings with exactly two decimals: prices from 1.00 to 5000.00,
sharesfrom 0.01 to 5000.00,dollarsfrom 0.01 to 50000.00, starting holdings from 0.00 to 0.99 - At most 1,000 orders and 100 symbols per call
- Inputs are always valid
What comes after stage 1
The next stage opens once the tests for the one before it pass. Each raises the bar, the way an interviewer does in the room.
- Stage 2: Every dealing, in order. Every movement of the desk's stock has to be accounted for: with whom, which way, and in the order it happened.
The files you get
- Yours to edit: desk.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
Robinhood 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 Fractional Stock Inventory 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?
Robinhood. It is modelled on Robinhood's coding round, which runs without AI. Based on candidates' reports of Robinhood 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?
4 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, Robinhood's interviews, the AI coding interview guide.