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 29, 2025 06:10Josu San MartinJosu San MartinScore: 3,586Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 29, 2025 06:073,02770,893,01536,322,0003,026,0002,420,736
2Dec 29, 2025 06:093,20372,624,13839,612,0002,031,0002,437,120
3Dec 29, 2025 06:083,22374,160,61938,901,0002,992,0002,420,736
4Dec 29, 2025 06:083,33669,308,29638,323,0005,042,0002,392,064
5Dec 29, 2025 06:113,34165,366,64839,399,0004,040,0002,334,720
6Dec 29, 2025 06:103,39274,878,94142,098,0002,004,0002,396,160
7Dec 29, 2025 06:073,46170,668,90941,988,0002,999,0002,473,984
8Dec 29, 2025 06:113,48869,237,11242,323,0003,023,0002,408,448
9Dec 29, 2025 06:093,52976,743,80143,887,0001,994,0002,306,048
10Dec 29, 2025 06:123,57967,762,88344,510,0002,023,0002,461,696
11Dec 29, 2025 06:143,58678,688,42845,611,0001,013,0002,510,848
12Dec 29, 2025 06:113,74381,623,96847,646,0001,013,0002,310,144
13Dec 29, 2025 06:143,78374,554,01047,177,0002,007,0002,457,600
14Dec 29, 2025 06:143,85175,885,60145,058,0005,006,0002,490,368
15Dec 29, 2025 06:093,86483,062,03948,229,0002,009,0002,326,528
16Dec 29, 2025 06:073,87172,979,25447,299,0003,019,0002,400,256
17Dec 29, 2025 06:123,94981,905,32346,301,0005,032,0002,355,200
18Dec 29, 2025 06:104,03579,756,42149,430,0003,026,0002,457,600
19Dec 29, 2025 06:124,18683,370,03752,403,0002,015,0002,318,336
20Dec 29, 2025 06:084,27684,946,49851,548,0004,043,0002,469,888
21Dec 29, 2025 06:104,30485,238,08253,957,0001,998,0002,363,392