Cross-Margin Liquidation Engine yenw0d

Simulate a cross-margin derivatives engine: process trades, track account equity, and liquidate undercollateralized accounts as fast as possible.

Accounts are funded with a USD deposit and trade instruments whose prices change over time. When a price update causes an account’s equity to fall below 1% of its total position notional, the account must be liquidated.

Definitions

  • Account equity = balance + Σ(size_i * price_i) - Σ(total_paid_i)
  • Total position notional = Σ(|size_i| * price_i)
  • Total paid for instrument i = signed sum of trade_size * price_at_trade_time across all trades
  • Margin rule: liquidate if equity < total_notional / 100 after any price update

Input

One command per line on STDIN:

  • a <balance> – create account with USD balance (IDs start at 0, incrementing)
  • p <instrument_idx> <price> – set/update instrument price (0-indexed)
  • t <account_idx> <instrument_idx> <size> – trade size (signed) at current price
  • Final line: <account_idx> – query this account and terminate

Output

On each price update, liquidate all accounts violating the margin rule. For each liquidation, print:

liquidate <account_id> <equity> <position_notional>

Liquidation order: largest total position notional first, then account ID descending as tie-breaker. Liquidated accounts have their balance and positions cleared.

For the final query, print <equity> <notional> for the requested account.

Constraints

  • Accounts: <= 100,000
  • Instruments: <= 1,000
  • Price range: 100 to 1,000,000
  • Trade size range: 1 to 10,000

Example

a 100
p 0 100
t 0 0 10
p 0 90
0

Output:

liquidate 0 0 900
0 0
Back to listOct 11, 2025 23:42imotw2imotw2Score: 155,643Success
Source Code

Source code access is restricted. Log in to request access.

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 11, 2025 23:24124,3061,640,508,3491,608,986,0006,995,0002,269,184
2Oct 11, 2025 23:24131,1971,729,993,1681,703,556,0001,999,0002,392,064
3Oct 11, 2025 23:24134,8341,776,307,0611,745,848,0006,995,0002,326,528
4Oct 11, 2025 23:24135,8221,789,561,2711,758,698,0006,994,0002,428,928
5Oct 11, 2025 23:25142,8171,888,131,5031,851,624,0004,996,0002,330,624
6Oct 11, 2025 23:25150,3451,984,824,7011,950,492,0003,996,0002,273,280
7Oct 11, 2025 23:24151,4512,001,557,7321,965,869,0002,998,0002,351,104
8Oct 11, 2025 23:25153,7272,030,067,9881,994,456,0003,996,0002,449,408
9Oct 11, 2025 23:24154,2462,031,605,6052,000,205,0004,998,0002,347,008
10Oct 11, 2025 23:42155,6432,057,251,2842,018,370,0004,995,0002,437,120
11Oct 11, 2025 23:24156,9272,068,552,3422,036,048,0003,998,0002,482,176
12Oct 11, 2025 23:28157,6572,075,378,1402,043,550,0005,995,0002,482,176
13Oct 11, 2025 23:28159,3572,104,056,4962,065,647,0005,996,0002,437,120
14Oct 11, 2025 23:42161,0962,123,197,7152,087,246,0006,997,0002,445,312
15Oct 11, 2025 23:42162,4322,142,061,3552,107,615,0003,997,0002,441,216
16Oct 11, 2025 23:24165,0032,170,620,9462,141,036,0003,998,0002,416,640
17Oct 11, 2025 23:28178,8912,349,859,1092,320,591,0004,996,0002,367,488
18Oct 11, 2025 23:24193,5912,546,107,0302,511,683,0004,997,0002,482,176