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 listMar 20, 2026 05:37Yuriy LyfenkoYuriy LyfenkoScore: 3,176Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 20, 2026 05:282,62361,491,06231,087,0003,008,0002,269,184
2Mar 20, 2026 05:362,73059,508,45633,464,0002,028,0002,367,488
3Mar 20, 2026 05:372,74666,154,12129,579,0006,119,0002,494,464
4Mar 20, 2026 05:282,79659,434,04733,322,0003,029,0002,379,776
5Mar 20, 2026 05:372,84563,887,05429,992,0006,998,0002,416,640
6Mar 20, 2026 05:282,85361,246,89933,080,0004,009,0002,486,272
7Mar 20, 2026 05:372,91967,546,27932,955,0004,993,0002,396,160
8Mar 20, 2026 05:282,92973,448,90534,064,0004,007,0002,490,368
9Mar 20, 2026 05:282,94665,487,63835,276,0003,023,0002,293,760
10Mar 20, 2026 05:362,97763,477,42834,632,0004,074,0002,400,256
11Mar 20, 2026 05:372,97866,565,93034,639,0004,075,0002,306,048
12Mar 20, 2026 05:362,98866,218,43634,859,0003,983,0002,473,984
13Mar 20, 2026 05:282,99161,931,72733,896,0004,984,0002,478,080
14Mar 20, 2026 05:363,01863,961,47736,212,0003,017,0002,301,952
15Mar 20, 2026 05:283,04165,351,93934,460,0005,067,0002,351,104
16Mar 20, 2026 05:283,11867,570,60936,486,0004,054,0002,289,664
17Mar 20, 2026 05:363,12267,720,27635,510,0005,072,0002,486,272
18Mar 20, 2026 05:283,12467,458,47436,553,0004,061,0002,289,664
19Mar 20, 2026 05:373,13172,167,91637,645,0003,052,0002,486,272
20Mar 20, 2026 05:283,15263,189,46537,980,0002,998,0002,289,664
21Mar 20, 2026 05:373,17471,081,16138,246,0003,019,0002,383,872
22Mar 20, 2026 05:363,17672,585,55138,270,0003,021,0002,293,760
23Mar 20, 2026 05:283,17964,310,74037,300,0004,032,0002,506,752
24Mar 20, 2026 05:363,18268,988,02738,336,0003,026,0002,285,568
25Mar 20, 2026 05:373,18868,002,23938,412,0003,032,0002,359,296
26Mar 20, 2026 05:283,20172,823,38038,570,0003,045,0002,539,520
27Mar 20, 2026 05:363,21568,120,70138,814,0002,985,0002,437,120
28Mar 20, 2026 05:283,21671,262,23336,831,0004,977,0002,297,856
29Mar 20, 2026 05:373,23066,110,53937,986,0003,998,0002,412,544
30Mar 20, 2026 05:363,26265,688,09939,372,0003,028,0002,428,928
31Mar 20, 2026 05:363,28168,363,47939,610,0003,046,0002,457,600
32Mar 20, 2026 05:283,28168,132,51439,610,0003,046,0002,433,024
33Mar 20, 2026 05:373,30065,269,71635,916,0006,983,0002,449,408
34Mar 20, 2026 05:283,31767,249,26138,108,0005,014,0002,310,144
35Mar 20, 2026 05:373,34965,403,16139,484,0004,049,0002,445,312
36Mar 20, 2026 05:283,43877,957,92939,612,0005,078,0002,531,328
37Mar 20, 2026 05:363,51577,478,83039,605,0006,093,0002,318,336
38Mar 20, 2026 05:283,60975,653,94542,930,0003,993,0002,519,040
39Mar 20, 2026 05:283,62971,455,39144,163,0003,011,0002,494,464
40Mar 20, 2026 05:363,65669,973,24543,484,0004,045,0002,351,104
41Mar 20, 2026 05:373,68869,284,14943,947,0003,995,0002,297,856
42Mar 20, 2026 05:373,76577,245,23941,954,0006,992,0002,301,952