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 18:24Josu San MartinJosu San MartinScore: 13,709Success
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 18:2111,884183,574,873144,458,00010,031,0002,416,640
2Dec 25, 2025 19:2112,374187,070,691153,868,0006,994,0002,473,984
3Dec 25, 2025 18:2112,406191,265,053149,253,00012,020,0002,494,464
4Dec 25, 2025 19:2112,886193,553,547160,492,0007,021,0002,457,600
5Dec 25, 2025 19:2112,886197,061,057159,492,0008,024,0002,383,872
6Dec 25, 2025 18:2112,950194,911,425164,346,0004,008,0002,478,080
7Dec 25, 2025 18:2713,000196,730,105157,998,00010,999,0002,281,472
8Dec 25, 2025 18:2713,078198,174,523167,015,0003,000,0002,441,216
9Dec 25, 2025 19:2113,081201,629,307165,055,0005,001,0002,392,064
10Dec 25, 2025 19:2113,130202,212,489160,708,0009,981,0002,383,872
11Dec 25, 2025 18:2113,419201,520,769166,423,0008,020,0002,523,136
12Dec 25, 2025 19:2113,474201,964,586166,151,0009,008,0002,387,968
13Dec 25, 2025 19:2113,523209,572,255164,816,00010,987,0002,510,848
14Dec 25, 2025 18:2313,524205,516,014168,776,0007,032,0002,387,968
15Dec 25, 2025 18:2213,533207,331,481165,931,0009,995,0002,400,256
16Dec 25, 2025 18:2213,614203,783,728168,980,0007,999,0002,433,024
17Dec 25, 2025 19:2113,627204,058,718173,152,0004,003,0002,457,600
18Dec 25, 2025 18:2113,645205,022,673169,368,0008,017,0002,424,832
19Dec 25, 2025 19:2113,709206,422,762171,207,0007,008,0002,461,696
20Dec 25, 2025 18:2113,725203,222,069167,398,00011,026,0002,465,792
21Dec 25, 2025 18:2113,942208,784,715175,235,0006,008,0002,445,312
22Dec 25, 2025 18:2313,984210,573,365173,803,0007,990,0002,289,664
23Dec 25, 2025 18:2214,145215,485,348175,891,0007,995,0002,392,064
24Dec 25, 2025 19:2114,189209,575,295175,436,0009,022,0002,351,104
25Dec 25, 2025 18:2114,394215,147,222179,115,0008,005,0002,363,392
26Dec 25, 2025 18:2114,455222,423,808176,919,00010,995,0002,347,008
27Dec 25, 2025 18:2414,653221,316,298183,471,0007,018,0002,510,848
28Dec 25, 2025 18:2414,719215,725,384182,329,0009,016,0002,400,256
29Dec 25, 2025 18:2714,943224,105,331187,251,0007,009,0002,510,848
30Dec 25, 2025 18:2314,995222,306,320186,938,0007,997,0002,416,640
31Dec 25, 2025 18:2115,444228,400,182193,782,0006,992,0002,306,048
32Dec 25, 2025 19:2115,799234,212,070197,372,0008,015,0002,347,008
33Dec 25, 2025 18:2416,289248,378,632199,767,00011,986,0002,387,968
34Dec 25, 2025 18:2116,373238,023,672202,861,0009,993,0002,379,776
35Dec 25, 2025 18:2116,904250,021,966213,754,0005,993,0002,334,720
36Dec 25, 2025 19:2117,029252,388,207217,369,0004,006,0002,347,008