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 listMar 19, 2026 11:30Dominique GarmierDominique GarmierScore: 18,230Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 19, 2026 11:5216,224238,944,488203,914,0006,997,0002,334,720
2Mar 19, 2026 11:4816,269243,754,138199,468,00012,028,0002,273,280
3Mar 19, 2026 11:4616,907243,966,612206,809,00012,988,0002,371,584
4Mar 19, 2026 11:5217,118248,319,050217,526,0005,012,0002,338,816
5Mar 19, 2026 11:5017,202253,447,220214,646,0008,985,0002,482,176
6Mar 19, 2026 11:3417,310249,165,035214,028,00011,001,0002,351,104
7Mar 19, 2026 11:4517,545255,708,893223,082,0005,001,0002,355,200
8Mar 19, 2026 11:2917,549252,997,615216,131,00012,007,0002,510,848
9Mar 19, 2026 11:4517,559257,396,822219,258,0009,010,0002,277,376
10Mar 19, 2026 11:2917,750256,775,153218,765,00011,987,0002,383,872
11Mar 19, 2026 11:5018,062258,195,803223,819,00010,991,0002,265,088
12Mar 19, 2026 11:4818,092267,967,331227,195,0008,006,0002,330,624
13Mar 19, 2026 11:3418,230265,286,556227,988,0008,999,0002,306,048
14Mar 19, 2026 11:2918,489268,425,844231,340,0009,013,0002,363,392
15Mar 19, 2026 11:4618,545272,363,625233,081,0008,002,0002,396,160
16Mar 19, 2026 11:3018,892272,092,691233,568,00012,029,0002,486,272
17Mar 19, 2026 11:3419,064272,127,924241,833,0005,995,0002,510,848
18Mar 19, 2026 11:5219,132276,682,097237,727,00010,987,0002,371,584
19Mar 19, 2026 11:5019,205275,359,017237,686,00011,984,0002,347,008
20Mar 19, 2026 11:3019,440285,663,806240,728,00011,986,0002,519,040
21Mar 19, 2026 11:4819,498281,641,929242,453,00011,020,0002,412,544
22Mar 19, 2026 11:4620,038290,480,730251,483,0009,017,0002,408,448
23Mar 19, 2026 11:3020,485290,988,509253,288,00013,014,0002,519,040
24Mar 19, 2026 11:4520,780295,891,714259,129,00011,005,0002,334,720