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 11, 2025 23:04imotw2imotw2Score: 151,557Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 11, 2025 22:41114,8381,523,967,1841,488,893,0003,997,0002,445,312
2Oct 11, 2025 22:41118,5711,569,688,5971,537,422,0003,998,0002,359,296
3Oct 11, 2025 22:41133,4501,767,413,2481,730,854,0003,997,0002,461,696
4Oct 11, 2025 22:41134,1871,770,899,8371,738,437,0005,994,0002,465,792
5Oct 11, 2025 22:41137,0531,818,638,4581,778,697,0002,997,0002,502,656
6Oct 11, 2025 23:04147,9951,950,610,2211,917,938,0005,996,0002,375,680
7Oct 11, 2025 22:41149,2171,971,152,9061,934,825,0004,996,0002,269,184
8Oct 11, 2025 22:41150,1141,977,782,1991,944,489,0006,994,0002,310,144
9Oct 11, 2025 23:23150,7011,988,007,5281,957,113,0001,999,0002,338,816
10Oct 11, 2025 22:41151,5571,999,906,8281,967,249,0002,998,0002,453,504
11Oct 11, 2025 23:23151,7121,996,567,5101,967,263,0004,998,0002,498,560
12Oct 11, 2025 23:23163,6132,158,259,8612,122,968,0003,998,0002,510,848
13Oct 11, 2025 23:10164,8322,174,431,2532,138,819,0003,997,0002,281,472
14Oct 11, 2025 23:10166,5052,197,199,6482,160,573,0003,997,0002,273,280
15Oct 11, 2025 23:10167,8932,209,087,4932,175,616,0006,995,0002,457,600
16Oct 11, 2025 23:04169,6062,232,269,2802,197,880,0006,996,0002,347,008
17Oct 11, 2025 22:41172,5232,270,068,2092,239,795,0002,998,0002,465,792
18Oct 11, 2025 23:04187,2332,465,751,9632,432,027,0001,999,0002,301,952