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 15:58Josu San MartinJosu San MartinScore: 13,703Success
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 15:5610,035157,286,107128,443,0002,006,0002,461,696
2Dec 25, 2025 16:1211,369174,841,444144,802,0002,995,0002,375,680
3Dec 25, 2025 15:5711,628177,837,150146,163,0005,005,0002,281,472
4Dec 25, 2025 15:5612,066189,404,796152,864,0003,996,0002,383,872
5Dec 25, 2025 15:5912,139187,254,404154,814,0002,996,0002,416,640
6Dec 25, 2025 15:5612,430197,480,617159,578,0002,007,0002,478,080
7Dec 25, 2025 15:5712,548193,319,561161,119,0002,001,0002,301,952
8Dec 25, 2025 15:5912,656192,468,186161,519,0003,009,0002,449,408
9Dec 25, 2025 15:5612,693189,387,992160,010,0005,000,0002,514,944
10Dec 25, 2025 15:5912,825199,820,930162,736,0003,993,0002,269,184
11Dec 25, 2025 16:1212,873195,293,056164,338,0003,006,0002,514,944
12Dec 25, 2025 16:2712,891196,721,693164,574,0003,010,0002,269,184
13Dec 25, 2025 15:5713,119200,303,740167,543,0003,009,0002,498,560
14Dec 25, 2025 15:5713,136196,233,451164,772,0005,991,0002,514,944
15Dec 25, 2025 16:1213,137198,453,729169,783,000998,0002,310,144
16Dec 25, 2025 15:5613,191198,492,718167,473,0004,011,0002,449,408
17Dec 25, 2025 15:5913,195203,074,888168,524,0003,009,0002,527,232
18Dec 25, 2025 15:5913,213205,402,849168,778,0002,996,0002,469,888
19Dec 25, 2025 16:1213,258203,878,897168,352,0004,008,0002,502,656
20Dec 25, 2025 16:2713,376197,821,135167,896,0005,996,0002,510,848
21Dec 25, 2025 16:2713,390199,361,524171,066,0003,001,0002,433,024
22Dec 25, 2025 15:5613,424208,632,473171,509,0003,008,0002,428,928
23Dec 25, 2025 16:1213,571209,149,767173,418,0003,007,0002,396,160
24Dec 25, 2025 16:2713,605207,832,522171,869,0004,996,0002,433,024
25Dec 25, 2025 15:5913,611207,511,190173,941,0002,998,0002,293,760
26Dec 25, 2025 15:5613,703203,173,648175,139,0003,002,0002,502,656
27Dec 25, 2025 15:5913,749207,026,163175,746,0002,995,0002,297,856
28Dec 25, 2025 16:1213,771204,079,377176,023,0003,000,0002,306,048
29Dec 25, 2025 16:1213,846209,094,866174,003,0006,000,0002,510,848
30Dec 25, 2025 15:5713,937205,613,621178,179,0003,003,0002,355,200
31Dec 25, 2025 15:5613,987204,670,636180,831,000999,0002,306,048
32Dec 25, 2025 15:5914,073211,511,304176,954,0005,998,0002,322,432
33Dec 25, 2025 15:5714,116216,591,315178,498,0005,013,0002,387,968
34Dec 25, 2025 15:5714,252212,444,599180,270,0005,007,0002,347,008
35Dec 25, 2025 16:1214,255213,124,458185,314,00002,482,176
36Dec 25, 2025 16:1214,273213,013,072179,529,0006,017,0002,437,120
37Dec 25, 2025 15:5914,337209,877,804184,375,0002,004,0002,510,848
38Dec 25, 2025 15:5814,409213,775,188184,311,0003,005,0002,482,176
39Dec 25, 2025 16:1214,461215,741,830183,992,0003,999,0002,449,408
40Dec 25, 2025 16:1214,629214,025,611187,173,0003,002,0002,510,848
41Dec 25, 2025 15:5714,636220,862,330188,271,0002,002,0002,281,472
42Dec 25, 2025 16:1214,675219,887,399186,780,0003,995,0002,318,336
43Dec 25, 2025 16:1214,830225,045,443188,797,0003,995,0002,514,944
44Dec 25, 2025 16:1214,884222,568,276190,480,0003,007,0002,281,472
45Dec 25, 2025 15:5815,033226,546,891190,417,0005,010,0002,502,656
46Dec 25, 2025 15:5715,075223,138,668190,976,0004,999,0002,478,080
47Dec 25, 2025 16:1215,115228,869,928194,495,0002,005,0002,281,472
48Dec 25, 2025 16:2715,402226,217,017193,217,0007,007,0002,297,856
49Dec 25, 2025 15:5615,407225,440,126197,289,0003,004,0002,351,104
50Dec 25, 2025 15:5815,978233,168,065204,717,0002,995,0002,281,472
51Dec 25, 2025 16:2716,615246,371,620213,992,0001,999,0002,326,528