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 20, 2026 04:32Yuriy LyfenkoYuriy LyfenkoScore: 3,688Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 20, 2026 04:313,40968,904,83037,266,0007,050,0002,461,696
2Mar 20, 2026 04:283,41675,423,33441,378,0003,027,0002,367,488
3Mar 20, 2026 04:313,42472,871,17843,503,0001,011,0002,494,464
4Mar 20, 2026 04:313,43470,865,28640,579,0004,057,0002,502,656
5Mar 20, 2026 04:313,43875,531,80140,632,0004,063,0002,404,352
6Mar 20, 2026 04:313,46173,558,55639,994,0004,999,0002,293,760
7Mar 20, 2026 04:313,49470,806,27843,408,0002,018,0002,510,848
8Mar 20, 2026 04:313,52973,793,16939,897,0005,984,0002,502,656
9Mar 20, 2026 04:313,55876,047,91042,233,0004,022,0002,351,104
10Mar 20, 2026 04:313,55975,727,64042,246,0004,023,0002,457,600
11Mar 20, 2026 04:313,55976,087,63543,251,0003,017,0002,420,736
12Mar 20, 2026 04:283,58273,993,43641,509,0005,062,0002,273,280
13Mar 20, 2026 04:283,65877,717,69743,508,0004,047,0002,363,392
14Mar 20, 2026 04:283,66274,603,52142,539,0005,064,0002,482,176
15Mar 20, 2026 04:323,66373,984,22242,552,0005,065,0002,416,640
16Mar 20, 2026 04:313,68873,609,55542,950,0004,994,0002,322,432
17Mar 20, 2026 04:283,71973,242,68944,314,0004,028,0002,322,432
18Mar 20, 2026 04:323,74375,024,57246,628,0002,027,0002,449,408
19Mar 20, 2026 04:283,77272,530,65046,030,0003,001,0002,482,176
20Mar 20, 2026 04:323,77480,008,36743,055,0006,007,0002,433,024
21Mar 20, 2026 04:313,78880,947,01143,211,0006,029,0002,498,560
22Mar 20, 2026 04:313,86575,948,35445,219,0005,024,0002,338,816
23Mar 20, 2026 04:283,91278,569,38742,881,0007,978,0002,338,816
24Mar 20, 2026 04:313,91578,925,00042,910,0007,983,0002,478,080
25Mar 20, 2026 04:283,93880,401,88647,176,0004,014,0002,478,080
26Mar 20, 2026 04:283,99378,178,59845,925,0005,990,0002,482,176
27Mar 20, 2026 04:314,00683,852,81146,069,0006,009,0002,322,432
28Mar 20, 2026 04:314,00877,910,34749,103,0003,006,0002,473,984
29Mar 20, 2026 04:314,11484,280,99948,434,0005,045,0002,445,312
30Mar 20, 2026 04:314,46083,470,67752,979,0004,998,0002,342,912