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 26, 2025 22:32Josu San MartinJosu San MartinScore: 6,108Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 26, 2025 22:304,98990,151,09259,874,0004,989,0002,527,232
2Dec 26, 2025 22:305,02993,663,63562,354,0003,017,0002,510,848
3Dec 26, 2025 22:265,10298,062,39763,308,0003,014,0002,482,176
4Dec 26, 2025 22:305,25094,056,83664,241,0004,015,0002,449,408
5Dec 26, 2025 22:305,37398,893,06365,803,0004,049,0002,490,368
6Dec 26, 2025 22:265,45494,362,26768,910,0001,997,0002,318,336
7Dec 26, 2025 22:305,48996,308,98870,354,0001,005,0002,281,472
8Dec 26, 2025 22:265,49699,474,71969,440,0002,012,0002,486,272
9Dec 26, 2025 22:305,762102,455,56270,912,0003,995,0002,498,560
10Dec 26, 2025 22:305,792101,848,39270,282,0005,020,0002,387,968
11Dec 26, 2025 22:265,841102,885,10270,939,0004,995,0002,334,720
12Dec 26, 2025 22:305,850105,318,64270,049,0006,004,0002,510,848
13Dec 26, 2025 22:305,879102,987,89673,407,0003,016,0002,359,296
14Dec 26, 2025 22:305,907104,659,64370,802,0005,983,0002,404,352
15Dec 26, 2025 22:305,912103,999,51972,859,0003,992,0002,412,544
16Dec 26, 2025 22:305,931105,640,36373,092,0004,005,0002,301,952
17Dec 26, 2025 22:306,00198,473,92075,018,0003,000,0002,387,968
18Dec 26, 2025 22:266,064110,714,26175,838,0002,993,0002,412,544
19Dec 26, 2025 22:306,090107,980,22376,160,0003,006,0002,473,984
20Dec 26, 2025 22:336,108106,675,15575,389,0004,020,0002,461,696
21Dec 26, 2025 22:336,124108,137,76272,553,0007,053,0002,420,736
22Dec 26, 2025 22:306,140108,016,15573,838,0005,986,0002,289,664
23Dec 26, 2025 22:306,142110,865,72976,815,0003,032,0002,408,448
24Dec 26, 2025 22:266,178106,856,13675,297,0005,019,0002,347,008
25Dec 26, 2025 22:306,219107,803,84576,850,0003,992,0002,465,792
26Dec 26, 2025 22:306,240104,473,61778,113,0003,004,0002,420,736
27Dec 26, 2025 22:306,259107,590,10776,342,0005,022,0002,461,696
28Dec 26, 2025 22:326,273109,353,20878,532,0003,020,0002,490,368
29Dec 26, 2025 22:306,321107,217,63377,166,0005,010,0002,318,336
30Dec 26, 2025 22:266,352110,037,24079,555,0003,021,0002,281,472
31Dec 26, 2025 22:266,375114,899,09978,886,0003,994,0002,469,888
32Dec 26, 2025 22:306,450104,267,21377,860,0005,989,0002,400,256
33Dec 26, 2025 22:336,507112,809,53480,563,0004,028,0002,510,848
34Dec 26, 2025 22:326,563113,643,79481,302,0004,014,0002,424,832
35Dec 26, 2025 22:306,589117,987,06279,609,0006,046,0002,396,160
36Dec 26, 2025 22:266,602111,742,97579,833,0005,987,0002,461,696
37Dec 26, 2025 22:326,836120,463,98484,829,0004,039,0002,527,232
38Dec 26, 2025 22:307,450124,897,67393,858,0002,995,0002,277,376
39Dec 26, 2025 22:307,564123,349,34897,326,0001,003,0002,347,008