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 25, 2025 01:30Josu San MartinJosu San MartinScore: 14,599Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 25, 2025 01:3011,412167,603,430146,352,0002,004,0002,318,336
2Dec 25, 2025 01:3012,223186,021,145150,900,0007,994,0002,383,872
3Dec 25, 2025 01:3013,469198,959,009167,098,0008,004,0002,359,296
4Dec 25, 2025 01:3513,501203,543,353170,499,0005,014,0002,420,736
5Dec 25, 2025 01:3513,691206,090,562171,979,0005,999,0002,478,080
6Dec 25, 2025 01:3513,834206,163,623171,851,0007,993,0002,482,176
7Dec 25, 2025 01:3013,864212,602,829172,225,0008,010,0002,420,736
8Dec 25, 2025 01:3113,904211,187,418174,755,0005,991,0002,433,024
9Dec 25, 2025 01:3114,027212,370,420178,347,0004,007,0002,461,696
10Dec 25, 2025 01:2914,117213,922,769175,494,0008,022,0002,375,680
11Dec 25, 2025 01:3514,599220,578,475180,792,0008,989,0002,482,176
12Dec 25, 2025 01:2914,606215,274,898178,891,00010,993,0002,420,736
13Dec 25, 2025 01:3514,953220,173,104191,389,0003,006,0002,539,520
14Dec 25, 2025 01:2915,332229,918,238190,298,0009,014,0002,445,312
15Dec 25, 2025 01:3015,712235,539,069201,252,0003,003,0002,408,448
16Dec 25, 2025 01:3016,111240,009,499206,438,0003,006,0002,338,816
17Dec 25, 2025 01:3516,367242,562,761205,775,0006,992,0002,494,464
18Dec 25, 2025 01:3016,462242,676,396208,003,0006,000,0002,420,736
19Dec 25, 2025 01:3116,643242,773,005210,345,0006,009,0002,498,560
20Dec 25, 2025 01:3017,367254,850,265218,777,0006,992,0002,531,328
21Dec 25, 2025 01:3017,601258,991,608220,824,0007,993,0002,482,176