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 30, 2025 05:22Josu San MartinJosu San MartinScore: 3,388Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 30, 2025 05:032,88259,573,08834,429,0003,037,0002,367,488
2Dec 30, 2025 05:023,05465,879,39036,652,0003,054,0002,310,144
3Dec 30, 2025 05:193,08470,839,76836,089,0004,009,0002,428,928
4Dec 30, 2025 05:193,10164,126,27436,284,0004,031,0002,314,240
5Dec 30, 2025 05:273,11865,575,65638,506,0002,026,0002,400,256
6Dec 30, 2025 05:183,17469,915,79839,247,0002,012,0002,478,080
7Dec 30, 2025 15:593,20670,142,03040,662,0001,016,0002,424,832
8Dec 30, 2025 05:273,22670,620,00540,934,000998,0002,367,488
9Dec 30, 2025 05:273,29872,620,65039,887,0002,991,0002,334,720
10Dec 30, 2025 05:033,29967,941,52140,888,0001,994,0002,506,752
11Dec 30, 2025 05:183,30069,100,80339,910,0002,993,0002,371,584
12Dec 30, 2025 05:193,30873,124,62939,006,0004,000,0002,367,488
13Dec 30, 2025 05:223,30971,219,55039,011,0004,001,0002,367,488
14Dec 30, 2025 15:593,31570,578,00141,092,0002,004,0002,396,160
15Dec 30, 2025 05:183,33964,674,85540,383,0003,028,0002,404,352
16Dec 30, 2025 05:273,34467,441,77742,467,0001,011,0002,269,184
17Dec 30, 2025 05:193,35169,893,83340,528,0003,039,0002,289,664
18Dec 30, 2025 05:183,37671,276,93840,898,0002,992,0002,367,488
19Dec 30, 2025 05:193,38170,176,66840,957,0002,996,0002,326,528
20Dec 30, 2025 05:033,38872,510,43637,041,0007,007,0002,506,752
21Dec 30, 2025 05:193,39269,703,90238,078,0006,012,0002,428,928
22Dec 30, 2025 05:273,40769,086,85941,277,0003,020,0002,281,472
23Dec 30, 2025 05:183,41267,033,47942,344,0002,016,0002,310,144
24Dec 30, 2025 05:193,41966,502,16442,426,0002,020,0002,330,624
25Dec 30, 2025 05:273,43770,144,65740,615,0004,061,0002,412,544
26Dec 30, 2025 05:023,45269,782,57241,884,0002,991,0002,461,696
27Dec 30, 2025 05:193,48077,596,57243,235,0002,010,0002,453,504
28Dec 30, 2025 05:203,50270,021,85342,492,0003,035,0002,375,680
29Dec 30, 2025 05:203,57970,535,30646,530,00002,424,832
30Dec 30, 2025 05:273,61371,229,87143,977,0002,998,0002,285,568
31Dec 30, 2025 05:223,69072,467,93544,973,0002,998,0002,310,144
32Dec 30, 2025 15:593,73273,736,19941,439,0007,075,0002,482,176
33Dec 30, 2025 05:273,77076,592,11548,006,0001,000,0002,355,200
34Dec 30, 2025 05:223,80072,538,08246,374,0003,024,0002,355,200
35Dec 30, 2025 05:023,82080,076,94845,612,0004,054,0002,453,504
36Dec 30, 2025 05:183,82280,607,68247,655,0002,027,0002,392,064
37Dec 30, 2025 05:273,88372,639,88546,441,0004,038,0002,293,760
38Dec 30, 2025 05:203,97780,323,38549,671,0002,027,0002,314,240
39Dec 30, 2025 05:194,22082,712,51949,877,0004,987,0002,301,952