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 28, 2025 23:58Josu San MartinJosu San MartinScore: 5,388Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 28, 2025 23:554,66790,416,33055,612,0005,055,0002,338,816
2Dec 28, 2025 23:574,78390,830,12659,170,0003,008,0002,273,280
3Dec 28, 2025 23:554,83791,820,20759,882,0002,994,0002,306,048
4Dec 28, 2025 23:584,86393,796,97360,203,0003,010,0002,416,640
5Dec 28, 2025 23:594,99090,440,01262,876,0001,996,0002,404,352
6Dec 28, 2025 23:585,01789,708,84963,216,0002,006,0002,347,008
7Dec 28, 2025 23:585,09797,999,77662,248,0004,016,0002,510,848
8Dec 28, 2025 23:595,33797,619,93062,339,0007,038,0002,342,912
9Dec 28, 2025 23:575,33896,200,99865,371,0004,022,0002,506,752
10Dec 28, 2025 23:575,36997,115,55665,811,0003,988,0002,318,336
11Dec 28, 2025 23:595,38896,477,29765,047,0005,003,0002,273,280
12Dec 28, 2025 23:585,52699,457,46167,848,0003,991,0002,306,048
13Dec 28, 2025 23:585,817103,158,71573,601,0002,016,0002,301,952
14Dec 28, 2025 23:585,823102,484,54669,641,0006,055,0002,400,256
15Dec 28, 2025 23:585,875104,091,38072,361,0004,020,0002,416,640
16Dec 28, 2025 23:585,875105,422,43772,361,0004,020,0002,355,200
17Dec 28, 2025 23:585,920100,758,03973,968,0002,998,0002,412,544
18Dec 28, 2025 23:586,042109,283,75871,495,0007,048,0002,306,048
19Dec 28, 2025 23:556,112107,891,08476,441,0003,017,0002,338,816
20Dec 28, 2025 23:586,650113,931,27581,426,0005,026,0002,465,792
21Dec 28, 2025 23:586,802115,793,67585,413,0003,014,0002,338,816