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 06:13Josu San MartinJosu San MartinScore: 3,800Success
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 06:133,09265,215,02038,192,0002,010,0002,523,136
2Dec 29, 2025 06:133,10766,120,32038,367,0002,019,0002,293,760
3Dec 29, 2025 06:133,46178,680,87441,999,0002,999,0002,498,560
4Dec 29, 2025 06:153,48274,044,00342,251,0003,017,0002,375,680
5Dec 29, 2025 06:153,51471,480,25641,626,0004,061,0002,334,720
6Dec 29, 2025 06:103,55571,692,33342,199,0004,019,0002,445,312
7Dec 29, 2025 06:143,56570,302,91344,333,0002,015,0002,428,928
8Dec 29, 2025 06:143,59272,518,17742,635,0004,060,0002,441,216
9Dec 29, 2025 06:133,66074,414,76246,565,0001,012,0002,334,720
10Dec 29, 2025 06:103,66872,972,81246,667,0001,014,0002,400,256
11Dec 29, 2025 06:143,69577,132,82245,036,0003,002,0002,453,504
12Dec 29, 2025 06:113,70776,930,85746,178,0002,007,0002,334,720
13Dec 29, 2025 06:123,72969,769,03843,429,0005,049,0002,269,184
14Dec 29, 2025 06:133,74173,621,40645,595,0003,039,0002,326,528
15Dec 29, 2025 06:153,79477,147,81144,284,0005,032,0002,445,312
16Dec 29, 2025 06:123,79877,049,87845,347,0004,030,0002,363,392
17Dec 29, 2025 06:113,80074,757,83144,355,0005,040,0002,301,952
18Dec 29, 2025 06:113,81471,457,38646,552,0003,036,0002,408,448
19Dec 29, 2025 06:103,83780,644,41547,884,0001,995,0002,318,336
20Dec 29, 2025 06:133,84279,505,82546,944,0002,996,0002,355,200
21Dec 29, 2025 06:133,87477,558,88646,334,0004,029,0002,416,640
22Dec 29, 2025 06:113,87872,715,48546,379,0004,033,0002,375,680
23Dec 29, 2025 06:123,91376,870,95245,884,0004,987,0002,473,984
24Dec 29, 2025 06:113,94574,310,98949,269,0002,011,0002,289,664
25Dec 29, 2025 06:133,97877,199,06650,696,0001,013,0002,424,832
26Dec 29, 2025 06:123,99075,392,30148,873,0002,992,0002,330,624
27Dec 29, 2025 06:114,01781,920,26648,208,0004,017,0002,416,640
28Dec 29, 2025 06:134,03375,763,91347,384,0005,040,0002,273,280
29Dec 29, 2025 06:134,06683,539,23148,866,0003,989,0002,473,984
30Dec 29, 2025 06:124,22782,078,49651,952,0002,997,0002,404,352
31Dec 29, 2025 06:134,25784,680,76551,314,0004,024,0002,326,528
32Dec 29, 2025 06:134,27283,086,77851,492,0004,038,0002,510,848
33Dec 29, 2025 06:124,39082,705,63453,064,0004,004,0002,273,280