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 19, 2025 18:28Josu San MartinJosu San MartinScore: 66,645Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 19, 2025 18:2852,474706,375,771678,158,0004,000,0002,506,752
2Dec 19, 2025 18:1359,001793,791,800760,013,0007,000,0002,416,640
3Dec 19, 2025 18:1360,076813,223,479776,992,0003,999,0002,469,888
4Dec 19, 2025 18:2862,167834,477,201804,168,0004,000,0002,453,504
5Dec 19, 2025 18:2862,341834,263,967804,441,0005,995,0002,359,296
6Dec 19, 2025 18:1362,886846,545,732808,518,0008,994,0002,506,752
7Dec 19, 2025 18:2865,688883,631,268848,944,0004,999,0002,514,944
8Dec 19, 2025 18:1366,231886,263,915856,007,0005,000,0002,469,888
9Dec 19, 2025 18:1366,567890,309,936860,379,0004,996,0002,510,848
10Dec 19, 2025 18:1366,645893,117,285861,383,0004,996,0002,289,664
11Dec 19, 2025 18:1368,029909,786,575879,381,0004,996,0002,510,848
12Dec 19, 2025 18:2869,665934,883,110900,648,0004,998,0002,445,312
13Dec 19, 2025 18:2871,713958,320,045927,269,0005,001,0002,473,984
14Dec 19, 2025 18:2874,483996,143,089962,278,0005,995,0002,297,856
15Dec 19, 2025 18:1375,3691,009,713,868972,802,0006,998,0002,412,544
16Dec 19, 2025 18:2879,2331,053,523,0381,026,026,0004,000,0002,482,176
17Dec 19, 2025 18:1381,1081,078,677,2081,049,413,0004,997,0002,482,176
18Dec 19, 2025 18:2897,2411,288,351,7071,259,132,0004,996,0002,273,280