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 26, 2025 22:34Josu San MartinJosu San MartinScore: 6,929Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 26, 2025 22:255,669103,098,79069,658,0004,038,0002,277,376
2Dec 26, 2025 22:255,897106,693,30270,613,0006,052,0002,408,448
3Dec 26, 2025 22:335,977108,712,34774,679,0003,027,0002,338,816
4Dec 26, 2025 22:256,167115,135,34876,162,0004,008,0002,437,120
5Dec 26, 2025 22:346,320112,380,52477,151,0005,009,0002,342,912
6Dec 26, 2025 22:256,400110,583,11678,190,0005,012,0002,383,872
7Dec 26, 2025 22:256,561122,043,15480,274,0005,017,0002,510,848
8Dec 26, 2025 22:256,719111,940,21084,333,0003,011,0002,416,640
9Dec 26, 2025 22:346,867114,503,07185,265,0004,012,0002,314,240
10Dec 26, 2025 22:256,897123,078,56584,626,0005,037,0002,392,064
11Dec 26, 2025 22:256,929116,377,79586,073,0004,003,0002,322,432
12Dec 26, 2025 22:346,950117,973,36484,324,0006,023,0002,486,272
13Dec 26, 2025 22:336,967119,177,90585,540,0005,031,0002,273,280
14Dec 26, 2025 22:347,185125,021,95387,375,0006,025,0002,293,760
15Dec 26, 2025 22:257,217118,730,69490,828,0002,994,0002,506,752
16Dec 26, 2025 22:257,253116,400,89787,262,0007,021,0002,469,888
17Dec 26, 2025 22:337,413123,081,37392,358,0004,015,0002,445,312
18Dec 26, 2025 22:347,456120,021,79390,928,0005,995,0002,449,408
19Dec 26, 2025 22:347,678127,252,16791,828,0007,985,0002,355,200
20Dec 26, 2025 22:257,770122,468,67395,004,0006,000,0002,375,680
21Dec 26, 2025 22:258,335135,336,810103,341,0005,016,0002,293,760