Problem · Database
Track the Book and Mark Final PnL
Problem statement
A market-making desk fills client requests and tracks its cash and inventory. Every row in fills is one completed client trade.
Book updates
- For a client
BUY, the desk sells the quantity: desk inventory decreases and cash increases byquantity * fill_price. - For a client
SELL, the desk buys the quantity: desk inventory increases and cash decreases byquantity * fill_price.
The closing_prices table represents the final price row, with exactly one closing mark for every traded asset. After all fills, compute pnl = cash + sum(inventory[asset] * mark_price[asset]).
Return exactly one row with the column pnl.
Table schema
MySQLPostgreSQLPandas
Use the same input data with any supported language. Open the Schema tab in the editor to see the generated SQL setup or Pandas DataFrames.
fills
| Column | Type | Nullable | Description |
|---|---|---|---|
| fill_idPK | Integer | No | — |
| asset | Text | No | — |
| client_side | Text | No | — |
| quantity | Integer | No | — |
| fill_price | Decimal | No | — |
closing_prices
| Column | Type | Nullable | Description |
|---|---|---|---|
| asset | Text | No | — |
| mark_price | Decimal | No | — |
Expected result
Your query or function must return these columns.
| Column | Type | Nullable | Description |
|---|---|---|---|
| pnl | Decimal | No | — |
Row order: must match exactly. Numeric tolerance: 0.
Constraints
0 <= fills.row_count <= 2000.- Every
fill_idis unique. - Every
client_sideis exactlyBUYorSELL. - Every
quantityis an integer in[1, 1000000]. - Every fill and closing price is an exact decimal in
(0, 1000000000000]with at most6fractional digits. closing_pricescontains exactly one row for each asset that occurs infills; it may also contain untraded assets.- Each fill notional is at most
10^18; the absolute cash and marked-inventory totals are each at most2 * 10^21, and|pnl| <= 4 * 10^21. These bounds keep every exact intermediate and result within the sharedDECIMAL(38, 12)/ 38-digit decimal runtime contract. - Use exact decimal arithmetic and return
0when there are no fills.