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 29, 2025 06:45Josu San MartinJosu San MartinScore: 3,671Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 29, 2025 06:403,10868,248,69135,352,0005,050,0002,363,392
2Dec 29, 2025 06:383,16165,708,73637,080,0004,008,0002,433,024
3Dec 29, 2025 06:403,25963,812,14738,335,0004,035,0002,342,912
4Dec 29, 2025 06:433,30168,195,87239,914,0002,993,0002,314,240
5Dec 29, 2025 06:443,31474,043,41539,073,0004,007,0002,318,336
6Dec 29, 2025 06:443,33464,329,49638,302,0005,039,0002,473,984
7Dec 29, 2025 06:383,34067,361,14838,375,0005,049,0002,404,352
8Dec 29, 2025 06:443,40768,637,92840,261,0004,026,0002,375,680
9Dec 29, 2025 06:403,41165,591,77041,316,0003,023,0002,396,160
10Dec 29, 2025 06:443,45170,907,69242,875,0001,994,0002,396,160
11Dec 29, 2025 06:463,46469,456,83741,033,0004,003,0002,367,488
12Dec 29, 2025 06:393,48275,963,78743,251,0002,011,0002,289,664
13Dec 29, 2025 06:413,49175,227,94342,361,0003,025,0002,412,544
14Dec 29, 2025 06:413,49469,134,90740,375,0005,046,0002,355,200
15Dec 29, 2025 06:423,49774,871,85343,440,0002,020,0002,281,472
16Dec 29, 2025 06:403,49870,576,20240,417,0005,052,0002,396,160
17Dec 29, 2025 06:433,51576,357,89545,693,00002,473,984
18Dec 29, 2025 06:443,53374,762,83343,891,0002,041,0002,396,160
19Dec 29, 2025 06:423,53572,234,80343,963,0001,998,0002,322,432
20Dec 29, 2025 06:423,55672,915,53742,208,0004,019,0002,342,912
21Dec 29, 2025 06:443,55673,578,31243,219,0003,015,0002,465,792
22Dec 29, 2025 06:383,56370,699,97644,308,0002,014,0002,293,760
23Dec 29, 2025 06:403,56574,584,36042,313,0004,029,0002,453,504
24Dec 29, 2025 06:453,57870,450,22444,488,0002,022,0002,367,488
25Dec 29, 2025 06:453,59671,806,32244,712,0002,032,0002,469,888
26Dec 29, 2025 06:403,60876,996,44743,841,0003,058,0002,293,760
27Dec 29, 2025 06:383,61679,094,88143,011,0004,001,0002,355,200
28Dec 29, 2025 06:443,62971,429,90345,176,0002,007,0002,314,240
29Dec 29, 2025 06:403,63172,622,13143,191,0004,017,0002,310,144
30Dec 29, 2025 06:403,65170,473,74444,433,0003,029,0002,383,872
31Dec 29, 2025 06:393,65671,144,70942,473,0005,056,0002,506,752
32Dec 29, 2025 06:393,67176,547,07346,706,0001,015,0002,383,872
33Dec 29, 2025 06:383,68277,988,36043,873,0003,988,0002,371,584
34Dec 29, 2025 06:393,71274,619,94245,238,0003,015,0002,510,848
35Dec 29, 2025 06:463,71480,381,83845,263,0003,017,0002,326,528
36Dec 29, 2025 06:423,72873,102,26246,443,0002,019,0002,375,680
37Dec 29, 2025 06:443,73370,510,46147,520,0001,011,0002,322,432
38Dec 29, 2025 06:413,74072,239,80445,576,0003,038,0002,482,176
39Dec 29, 2025 06:453,75875,393,86944,871,0003,988,0002,293,760
40Dec 29, 2025 06:463,75974,201,88447,866,000997,0002,310,144
41Dec 29, 2025 06:443,76882,697,25944,987,0003,998,0002,375,680
42Dec 29, 2025 06:413,77682,470,95746,077,0003,005,0002,420,736
43Dec 29, 2025 06:463,78474,837,22246,181,0003,011,0002,453,504
44Dec 29, 2025 06:423,79977,119,12546,367,0003,023,0002,457,600
45Dec 29, 2025 06:443,80482,128,67142,388,0007,064,0002,359,296
46Dec 29, 2025 06:443,80972,178,57346,487,0003,031,0002,371,584
47Dec 29, 2025 06:463,83575,392,30346,866,0002,991,0002,510,848
48Dec 29, 2025 06:433,84982,064,28546,040,0004,003,0002,400,256
49Dec 29, 2025 06:463,85878,465,69347,144,0003,009,0002,355,200
50Dec 29, 2025 06:453,86676,364,07645,231,0005,025,0002,273,280
51Dec 29, 2025 06:403,90175,689,38944,630,0006,085,0002,330,624
52Dec 29, 2025 06:403,91375,493,55745,879,0004,986,0002,453,504
53Dec 29, 2025 06:423,93481,430,87346,126,0005,013,0002,363,392
54Dec 29, 2025 06:453,94084,787,66649,212,0002,008,0002,482,176
55Dec 29, 2025 06:453,96988,814,22850,582,0001,011,0002,473,984
56Dec 29, 2025 06:413,99177,067,95547,897,0003,991,0002,306,048
57Dec 29, 2025 06:404,00082,111,62848,995,0002,999,0002,445,312
58Dec 29, 2025 06:414,07380,759,23446,954,0005,994,0002,510,848
59Dec 29, 2025 06:404,09880,759,26149,259,0004,021,0002,334,720
60Dec 29, 2025 06:394,14781,737,57649,914,0003,993,0002,383,872
61Dec 29, 2025 06:444,15685,357,32352,030,0002,001,0002,502,656
62Dec 29, 2025 06:394,22080,399,22953,863,000997,0002,461,696
63Dec 29, 2025 06:384,26484,913,91250,390,0005,039,0002,371,584