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 17:35Josu San MartinJosu San MartinScore: 6,684Success
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 17:275,466106,654,57166,053,0005,004,0002,342,912
2Dec 26, 2025 17:275,63599,660,84669,243,0004,014,0002,408,448
3Dec 26, 2025 17:355,68298,917,52364,883,0008,983,0002,379,776
4Dec 26, 2025 17:265,987106,947,07972,847,0004,989,0002,412,544
5Dec 26, 2025 17:276,106111,937,68173,352,0006,028,0002,527,232
6Dec 26, 2025 17:276,156113,072,18073,021,0007,002,0002,363,392
7Dec 26, 2025 17:276,224107,138,42874,923,0005,993,0002,342,912
8Dec 26, 2025 17:356,234106,900,43179,043,0002,001,0002,342,912
9Dec 26, 2025 17:276,246106,835,42874,179,0007,017,0002,510,848
10Dec 26, 2025 17:356,272106,796,13878,512,0003,019,0002,310,144
11Dec 26, 2025 22:046,276108,267,38178,567,0003,021,0002,502,656
12Dec 26, 2025 17:356,294108,393,76273,835,0007,982,0002,465,792
13Dec 26, 2025 17:356,358110,268,11578,626,0004,032,0002,441,216
14Dec 26, 2025 17:276,474113,907,43377,147,0007,013,0002,473,984
15Dec 26, 2025 17:276,476121,556,57879,174,0005,011,0002,383,872
16Dec 26, 2025 17:276,557113,599,97380,232,0005,014,0002,301,952
17Dec 26, 2025 17:266,684111,361,97977,908,0008,989,0002,351,104
18Dec 26, 2025 17:276,708117,188,37783,190,0004,009,0002,506,752
19Dec 26, 2025 17:356,712119,139,21785,248,0002,005,0002,301,952
20Dec 26, 2025 17:276,762113,270,26784,910,0002,996,0002,404,352
21Dec 26, 2025 17:276,795121,446,97984,326,0004,015,0002,441,216
22Dec 26, 2025 22:046,800119,285,23183,381,0005,023,0002,506,752
23Dec 26, 2025 17:266,837112,550,97282,890,0005,992,0002,379,776
24Dec 26, 2025 22:046,844119,569,60884,974,0003,998,0002,338,816
25Dec 26, 2025 17:266,852117,389,99884,074,0005,004,0002,281,472
26Dec 26, 2025 17:357,087117,892,31887,128,0005,007,0002,445,312
27Dec 26, 2025 17:357,327119,913,28192,240,0003,007,0002,293,760
28Dec 26, 2025 17:357,428124,703,79289,525,0007,041,0002,400,256
29Dec 26, 2025 17:357,530123,979,44688,900,0008,989,0002,326,528
30Dec 26, 2025 17:357,779127,424,61795,116,0006,007,0002,433,024
31Dec 26, 2025 17:267,801131,865,76294,383,0007,028,0002,375,680
32Dec 26, 2025 17:267,812132,248,67798,534,0003,016,0002,494,464
33Dec 26, 2025 17:358,225132,344,68299,926,0006,994,0002,310,144