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 listDec 26, 2025 23:25Josu San MartinJosu San MartinScore: 5,462Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 26, 2025 23:274,70988,599,71353,185,0008,027,0002,277,376
2Dec 26, 2025 23:274,98989,283,18859,869,0004,989,0002,396,160
3Dec 26, 2025 23:275,02993,784,08163,361,0002,011,0002,469,888
4Dec 26, 2025 23:265,06391,896,93261,835,0003,989,0002,465,792
5Dec 26, 2025 23:275,12993,656,15859,605,0007,071,0002,502,656
6Dec 26, 2025 23:255,18894,460,69064,428,0003,020,0002,314,240
7Dec 26, 2025 23:275,21997,653,02165,852,0001,995,0002,461,696
8Dec 26, 2025 23:265,30793,893,15360,987,0007,998,0002,281,472
9Dec 26, 2025 23:275,367105,778,73864,790,0004,983,0002,416,640
10Dec 26, 2025 23:275,374104,264,59565,869,0003,992,0002,375,680
11Dec 26, 2025 23:275,42797,349,46666,515,0004,031,0002,510,848
12Dec 26, 2025 23:255,43698,924,92666,630,0004,038,0002,506,752
13Dec 26, 2025 23:275,449101,018,75765,844,0004,988,0002,396,160
14Dec 26, 2025 23:275,46297,384,13165,001,0006,000,0002,465,792
15Dec 26, 2025 23:275,562104,305,88966,281,0006,025,0002,383,872
16Dec 26, 2025 23:275,578101,372,24968,483,0004,028,0002,465,792
17Dec 26, 2025 23:255,627106,410,00466,141,0007,014,0002,469,888
18Dec 26, 2025 23:275,628100,600,31572,166,0001,002,0002,510,848
19Dec 26, 2025 23:255,785105,083,56269,189,0006,016,0002,523,136
20Dec 26, 2025 23:275,841103,130,99174,937,000999,0002,371,584
21Dec 26, 2025 23:255,884106,892,20575,490,0001,006,0002,330,624
22Dec 26, 2025 23:255,905113,027,46470,779,0005,981,0002,379,776
23Dec 26, 2025 23:276,142108,918,75375,851,0003,992,0002,473,984
24Dec 26, 2025 23:276,152109,482,21875,979,0003,998,0002,514,944
25Dec 26, 2025 23:276,194108,341,51374,489,0006,039,0002,314,240
26Dec 26, 2025 23:266,204108,308,35678,634,0002,016,0002,494,464
27Dec 26, 2025 23:276,602112,987,45683,825,0001,995,0002,506,752