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 22, 2025 22:50Josu San MartinJosu San MartinScore: 61,584Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 22, 2025 16:5353,222716,352,295681,884,0009,998,0002,506,752
2Dec 22, 2025 19:3954,662742,694,531702,613,0007,995,0002,465,792
3Dec 22, 2025 16:5356,946771,412,889731,293,0009,003,0002,363,392
4Dec 22, 2025 16:1057,417770,268,215739,422,0006,994,0002,531,328
5Dec 22, 2025 22:5057,909780,770,039746,823,0005,998,0002,428,928
6Dec 22, 2025 19:3958,041785,376,480746,543,0007,995,0002,359,296
7Dec 22, 2025 16:5358,569785,672,565754,404,0006,994,0002,342,912
8Dec 22, 2025 16:5358,816794,217,940756,612,0007,995,0002,404,352
9Dec 22, 2025 22:5059,152794,839,566761,976,0006,999,0002,498,560
10Dec 22, 2025 22:5060,439810,758,143777,716,0007,997,0002,482,176
11Dec 22, 2025 16:1060,533818,636,130778,931,0007,999,0002,531,328
12Dec 22, 2025 16:5361,268821,564,899792,482,0003,997,0002,416,640
13Dec 22, 2025 16:5361,570829,791,337796,417,0003,997,0002,465,792
14Dec 23, 2025 03:5861,584829,194,534794,599,0005,996,0002,342,912
15Dec 23, 2025 03:5863,033846,320,382814,433,0004,996,0002,367,488
16Dec 22, 2025 16:5363,698854,656,524822,073,0006,000,0002,342,912
17Dec 22, 2025 22:5064,508865,845,433832,612,0005,997,0002,293,760
18Dec 22, 2025 22:5065,952887,592,884849,386,0007,994,0002,473,984
19Dec 23, 2025 03:5866,043881,191,144851,566,0006,996,0002,404,352
20Dec 22, 2025 22:5066,066885,803,150852,856,0005,998,0002,301,952
21Dec 22, 2025 22:5066,342889,872,725855,445,0006,995,0002,326,528
22Dec 22, 2025 16:5366,752896,550,927863,779,0003,998,0002,412,544
23Dec 22, 2025 16:5367,665909,480,090870,643,0008,996,0002,379,776
24Dec 22, 2025 19:3968,876919,981,188890,387,0004,996,0002,400,256
25Dec 22, 2025 16:1069,307926,893,522896,988,0003,999,0002,445,312
26Dec 22, 2025 22:5075,5911,007,411,368976,687,0005,998,0002,330,624
27Dec 22, 2025 22:5079,2691,060,332,6111,020,508,0009,995,0002,519,040