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 12, 2025 23:08imotw2imotw2Score: 27,114Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 12, 2025 22:5823,256326,817,231298,321,0004,004,0002,490,368
2Oct 12, 2025 23:0823,931335,770,529306,107,0005,001,0002,281,472
3Oct 12, 2025 22:5825,920362,131,548333,956,0002,999,0002,371,584
4Oct 12, 2025 22:5826,540372,824,434341,020,0004,000,0002,433,024
5Oct 12, 2025 23:0826,628373,180,426344,166,0002,000,0002,310,144
6Oct 12, 2025 23:0226,708371,820,706340,196,0007,004,0002,478,080
7Oct 12, 2025 23:0826,945374,826,115347,277,0003,002,0002,301,952
8Oct 12, 2025 22:5827,003374,760,995348,036,0003,000,0002,404,352
9Oct 12, 2025 23:0827,095384,910,593348,230,0004,002,0002,469,888
10Oct 12, 2025 23:0227,105378,061,799346,374,0005,989,0002,486,272
11Oct 12, 2025 22:5827,114379,978,850346,471,0006,008,0002,277,376
12Oct 12, 2025 23:0227,201378,692,743347,626,0005,993,0002,383,872
13Oct 12, 2025 23:0827,272382,874,426351,541,0002,996,0002,433,024
14Oct 12, 2025 23:0827,599388,936,744353,788,0004,997,0002,367,488
15Oct 12, 2025 22:5828,247392,942,445363,215,0004,002,0002,498,560
16Oct 12, 2025 23:0828,734404,857,891369,553,0003,995,0002,326,528
17Oct 12, 2025 22:5828,877404,744,901372,395,0003,003,0002,445,312
18Oct 12, 2025 23:0829,078402,308,716375,008,0003,000,0002,482,176
19Oct 12, 2025 23:0829,194406,860,169373,536,0005,992,0002,330,624
20Oct 12, 2025 22:5830,783424,940,998397,179,0003,001,0002,490,368
21Oct 12, 2025 22:5831,407443,563,745406,291,0002,001,0002,326,528