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 15:42Josu San MartinJosu San MartinScore: 3,688Success
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 19:053,26865,291,51839,449,0003,034,0002,408,448
2Dec 29, 2025 19:053,27868,160,80639,573,0003,044,0002,256,896
3Dec 29, 2025 15:433,48968,144,52542,336,0003,024,0002,334,720
4Dec 29, 2025 19:053,57068,608,90743,386,0003,026,0002,367,488
5Dec 29, 2025 15:423,60575,359,61046,870,00002,265,088
6Dec 29, 2025 19:053,61771,184,44945,026,0002,001,0002,453,504
7Dec 29, 2025 19:053,62374,993,35044,099,0003,006,0002,404,352
8Dec 29, 2025 19:053,64172,379,03045,318,0002,014,0002,363,392
9Dec 29, 2025 20:143,64884,011,60443,385,0004,035,0002,326,528
10Dec 29, 2025 19:053,68882,555,10745,943,0001,997,0002,342,912
11Dec 29, 2025 20:143,72173,168,42944,339,0004,030,0002,482,176
12Dec 29, 2025 15:433,92075,354,70945,966,0004,996,0002,281,472
13Dec 29, 2025 15:433,99180,425,29948,891,0002,993,0002,494,464
14Dec 29, 2025 19:054,03578,991,67648,416,0004,034,0002,359,296
15Dec 29, 2025 15:424,04878,993,28950,603,0002,024,0002,310,144
16Dec 29, 2025 20:144,10781,291,90849,361,0004,029,0002,338,816
17Dec 29, 2025 19:054,11176,883,58749,414,0004,033,0002,453,504
18Dec 29, 2025 15:424,15982,079,23352,068,0002,002,0002,306,048