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 listOct 9, 2025 14:58Yuriy LyfenkoYuriy LyfenkoScore: 7,248Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 9, 2025 15:016,078104,432,39774,014,0005,000,0002,314,240
2Oct 9, 2025 15:016,382115,399,76179,969,0002,998,0002,473,984
3Oct 9, 2025 15:006,409113,234,67878,299,0005,019,0002,277,376
4Oct 9, 2025 15:006,469110,145,79779,088,0005,005,0002,289,664
5Oct 8, 2025 20:296,596114,825,03080,757,0004,985,0002,379,776
6Oct 8, 2025 20:296,681113,333,34584,851,0001,996,0002,265,088
7Oct 9, 2025 15:016,774114,412,82084,056,0004,002,0002,437,120
8Oct 9, 2025 15:016,854114,746,71685,098,0004,004,0002,502,656
9Oct 9, 2025 14:586,904115,613,03784,762,0004,986,0002,301,952
10Oct 8, 2025 20:297,024117,508,64687,299,0004,013,0002,486,272
11Oct 9, 2025 15:007,096119,997,71888,233,0004,010,0002,482,176
12Oct 9, 2025 15:007,112117,292,18686,426,0006,029,0002,351,104
13Oct 8, 2025 20:297,137125,016,11085,801,0006,983,0002,498,560
14Oct 9, 2025 15:007,229120,046,10089,983,0003,999,0002,269,184
15Oct 9, 2025 15:007,238125,782,17090,087,0004,003,0002,510,848
16Oct 9, 2025 15:017,248126,079,70489,216,0005,012,0002,449,408
17Oct 9, 2025 15:007,373125,560,33890,858,0004,992,0002,457,600
18Oct 8, 2025 20:297,375125,021,25891,883,0003,994,0002,514,944
19Oct 9, 2025 14:587,381127,412,33790,956,0004,997,0002,510,848
20Oct 9, 2025 15:017,396120,803,91791,139,0005,007,0002,338,816
21Oct 9, 2025 15:017,399121,196,47094,185,0002,003,0002,494,464
22Oct 9, 2025 15:007,889129,270,90698,539,0004,022,0002,465,792
23Oct 9, 2025 15:017,952130,160,81398,354,0005,018,0002,416,640
24Oct 9, 2025 15:007,990137,139,74799,877,0003,995,0002,490,368
25Oct 8, 2025 20:298,076131,880,85599,983,0004,999,0002,396,160
26Oct 9, 2025 14:588,077129,536,585101,996,0002,999,0002,392,064
27Oct 8, 2025 20:298,098133,822,589100,263,0005,013,0002,392,064
28Oct 9, 2025 15:018,236132,796,749104,063,0003,001,0002,461,696
29Oct 8, 2025 20:298,662138,765,297107,585,0005,027,0002,437,120
30Oct 8, 2025 20:299,663156,715,696121,594,0004,019,0002,433,024