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 25, 2025 18:19Josu San MartinJosu San MartinScore: 14,212Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 25, 2025 18:1511,677174,702,851145,815,0005,992,0002,326,528
2Dec 25, 2025 18:1511,992192,885,144150,901,0004,996,0002,383,872
3Dec 25, 2025 18:1812,223181,214,774155,902,0002,998,0002,273,280
4Dec 25, 2025 16:3612,267182,628,213156,460,0003,008,0002,269,184
5Dec 25, 2025 18:1512,525198,316,235160,834,0001,997,0002,310,144
6Dec 25, 2025 16:3812,753195,721,366162,794,0002,996,0002,277,376
7Dec 25, 2025 18:1512,859198,117,034164,170,0003,003,0002,342,912
8Dec 25, 2025 18:1512,886199,489,179162,509,0005,015,0002,322,432
9Dec 25, 2025 18:1512,903195,832,699163,748,0003,993,0002,437,120
10Dec 25, 2025 16:3613,193199,180,267169,499,0002,005,0002,310,144
11Dec 25, 2025 18:1813,312203,600,798169,053,0004,001,0002,314,240
12Dec 25, 2025 18:2113,368200,830,715170,792,0002,996,0002,523,136
13Dec 25, 2025 18:1813,496201,216,772172,444,0003,007,0002,514,944
14Dec 25, 2025 18:1513,655212,585,037174,512,0003,008,0002,445,312
15Dec 25, 2025 18:2113,809212,206,592177,517,0002,005,0002,514,944
16Dec 25, 2025 18:1813,859206,545,725174,159,0006,005,0002,379,776
17Dec 25, 2025 18:1513,907212,094,208176,790,0003,995,0002,490,368
18Dec 25, 2025 18:1814,006213,942,155179,072,0003,001,0002,273,280
19Dec 25, 2025 18:1514,021215,159,005177,271,0005,007,0002,301,952
20Dec 25, 2025 16:3714,024211,549,988179,303,0003,005,0002,301,952
21Dec 25, 2025 16:3814,082216,039,187181,060,0002,000,0002,486,272
22Dec 25, 2025 18:2114,125210,296,853176,599,0007,023,0002,379,776
23Dec 25, 2025 16:3814,212212,633,801180,756,0003,994,0002,359,296
24Dec 25, 2025 16:3614,324209,824,152180,201,0006,006,0002,289,664
25Dec 25, 2025 18:1914,343209,238,181183,452,0003,007,0002,387,968
26Dec 25, 2025 16:3614,506216,827,278185,567,0003,009,0002,494,464
27Dec 25, 2025 16:3814,522219,010,262184,793,0003,995,0002,289,664
28Dec 25, 2025 16:3614,633217,871,722187,222,0003,003,0002,441,216
29Dec 25, 2025 18:1514,672219,649,932185,739,0004,993,0002,523,136
30Dec 25, 2025 16:3814,679215,144,430186,828,0003,996,0002,453,504
31Dec 25, 2025 18:1814,727218,616,103188,445,0003,007,0002,502,656
32Dec 25, 2025 18:1514,769215,445,771185,994,0005,999,0002,322,432
33Dec 25, 2025 18:1914,905226,891,625186,776,0006,991,0002,347,008
34Dec 25, 2025 18:1915,059223,615,962191,773,0003,995,0002,297,856
35Dec 25, 2025 18:1515,087227,798,192190,129,0006,004,0002,420,736
36Dec 25, 2025 16:3715,126224,695,674189,655,0006,987,0002,478,080
37Dec 25, 2025 18:1515,168216,744,533192,186,0005,004,0002,351,104
38Dec 25, 2025 16:3615,348233,954,684197,525,0002,005,0002,514,944
39Dec 25, 2025 18:1915,531229,967,864198,904,0002,998,0002,445,312
40Dec 25, 2025 18:1915,622232,219,055200,087,0003,001,0002,506,752
41Dec 25, 2025 16:3715,943229,726,602201,252,0006,007,0002,461,696
42Dec 25, 2025 18:1916,524238,951,038209,814,0004,995,0002,486,272
43Dec 25, 2025 18:1516,573245,380,335211,436,0004,008,0002,289,664
44Dec 25, 2025 16:3816,783243,354,985215,178,0003,002,0002,523,136
45Dec 25, 2025 18:1517,750259,301,023226,759,0003,995,0002,510,848