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 25, 2025 19:27Josu San MartinJosu San MartinScore: 13,656Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 25, 2025 19:2911,598182,876,146148,779,0001,997,0002,486,272
2Dec 25, 2025 19:2712,069182,567,751150,897,0005,995,0002,326,528
3Dec 25, 2025 19:2912,150185,970,418153,958,0003,998,0002,523,136
4Dec 25, 2025 19:2912,449191,797,969156,843,0004,995,0002,297,856
5Dec 25, 2025 19:2712,472195,582,071159,134,0003,002,0002,408,448
6Dec 25, 2025 19:2912,567190,926,172157,357,0006,013,0002,318,336
7Dec 25, 2025 19:2713,021193,231,791163,270,0006,009,0002,322,432
8Dec 25, 2025 19:2913,024194,640,566165,300,0004,007,0002,273,280
9Dec 25, 2025 19:2913,139194,688,971167,812,0002,996,0002,510,848
10Dec 25, 2025 19:2913,369208,592,273167,804,0005,993,0002,334,720
11Dec 25, 2025 19:2713,472205,244,846172,137,0003,002,0002,473,984
12Dec 25, 2025 19:2913,481198,029,414170,247,0005,007,0002,461,696
13Dec 25, 2025 19:2913,656209,557,599174,522,0003,009,0002,273,280
14Dec 25, 2025 19:2913,734208,775,686177,537,0001,003,0002,318,336
15Dec 25, 2025 19:2913,751202,337,954174,768,0003,994,0002,502,656
16Dec 25, 2025 19:2913,961209,008,732179,485,0002,005,0002,318,336
17Dec 25, 2025 19:2713,990205,078,588179,870,0001,998,0002,281,472
18Dec 25, 2025 19:2714,173214,318,097183,249,0001,001,0002,478,080
19Dec 25, 2025 19:2714,367217,622,658184,771,0001,997,0002,469,888
20Dec 25, 2025 19:2914,844216,954,672187,970,0004,999,0002,355,200
21Dec 25, 2025 19:2714,859223,979,889190,164,0003,002,0002,428,928
22Dec 25, 2025 19:2714,952217,834,644192,371,0002,003,0002,498,560
23Dec 25, 2025 19:2915,046228,795,217193,594,0002,006,0002,494,464
24Dec 25, 2025 19:2915,563224,354,259201,323,0001,001,0002,338,816