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 21:10Josu San MartinJosu San MartinScore: 14,303Success
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 21:1011,243178,497,562139,149,0007,007,0002,281,472
2Dec 25, 2025 21:1011,271178,349,603138,494,0008,028,0002,322,432
3Dec 25, 2025 21:1012,152189,308,120147,973,0009,998,0002,482,176
4Dec 25, 2025 21:1012,604191,599,228154,866,0008,992,0002,351,104
5Dec 25, 2025 21:1012,860198,496,559159,170,0008,008,0002,301,952
6Dec 25, 2025 21:0913,173199,477,695165,238,0006,008,0002,408,448
7Dec 25, 2025 21:0913,399202,421,962166,183,0008,008,0002,420,736
8Dec 25, 2025 21:1013,494207,860,947168,400,0007,016,0002,510,848
9Dec 25, 2025 21:0913,844206,379,541169,978,0009,998,0002,293,760
10Dec 25, 2025 21:1013,861209,054,746172,184,0008,008,0002,449,408
11Dec 25, 2025 21:1013,989214,879,665174,864,0006,994,0002,445,312
12Dec 25, 2025 21:1014,039211,648,157175,493,0007,019,0002,428,928
13Dec 25, 2025 21:1014,250211,611,126178,240,0007,009,0002,306,048
14Dec 25, 2025 21:0914,303212,787,001174,937,00010,996,0002,322,432
15Dec 25, 2025 21:1014,315219,041,922175,092,00011,005,0002,510,848
16Dec 25, 2025 21:1014,424216,150,106180,490,0007,019,0002,363,392
17Dec 25, 2025 21:1014,519218,214,952177,767,00010,985,0002,510,848
18Dec 25, 2025 21:1014,594223,699,770178,741,00010,984,0002,404,352
19Dec 25, 2025 21:1014,651213,874,278181,438,0009,021,0002,404,352
20Dec 25, 2025 21:0914,873221,006,889186,339,0007,012,0002,498,560
21Dec 25, 2025 21:0915,040230,996,945184,489,00011,029,0002,416,640
22Dec 25, 2025 21:1015,230226,281,107193,990,0003,999,0002,351,104
23Dec 25, 2025 21:1015,521229,781,259192,788,0008,990,0002,404,352
24Dec 25, 2025 21:1015,832234,168,297192,826,00012,988,0002,428,928
25Dec 25, 2025 21:1016,349240,511,115205,525,0007,017,0002,510,848
26Dec 25, 2025 21:1016,367244,638,418205,773,0006,992,0002,445,312
27Dec 25, 2025 21:1016,589243,122,931205,674,0009,984,0002,387,968