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 19:36Yuriy LyfenkoYuriy LyfenkoScore: 2,483Success
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 19:482,31664,622,94726,092,0004,014,0002,383,872
2Mar 20, 2026 19:362,31654,362,87025,090,0005,018,0002,265,088
3Mar 20, 2026 19:372,32456,573,99324,168,0006,042,0002,408,448
4Mar 20, 2026 19:362,32556,915,56625,183,0005,036,0002,408,448
5Mar 20, 2026 19:372,38059,231,53126,950,0003,992,0002,338,816
6Mar 20, 2026 19:372,39556,391,35229,131,0002,009,0002,433,024
7Mar 20, 2026 19:362,39660,727,14127,129,0004,019,0002,494,464
8Mar 20, 2026 19:482,40358,977,36827,207,0004,030,0002,486,272
9Mar 20, 2026 19:362,42055,164,26826,381,0005,073,0002,461,696
10Mar 20, 2026 19:362,43954,058,45127,613,0004,090,0002,392,064
11Mar 20, 2026 19:372,43953,996,82428,640,0003,068,0002,392,064
12Mar 20, 2026 19:372,44059,217,53226,600,0005,115,0002,428,928
13Mar 20, 2026 19:482,45461,452,01327,915,0003,987,0002,289,664
14Mar 20, 2026 19:482,45755,653,66024,954,0006,987,0002,318,336
15Mar 20, 2026 19:482,45957,757,28325,969,0005,992,0002,482,176
16Mar 20, 2026 19:482,48357,495,80528,239,0004,034,0002,322,432
17Mar 20, 2026 19:372,48760,187,82226,272,0006,062,0002,404,352
18Mar 20, 2026 19:372,49962,434,89526,393,0006,090,0002,322,432
19Mar 20, 2026 19:482,50660,608,77626,467,0006,107,0002,449,408
20Mar 20, 2026 19:362,51255,594,03427,551,0005,102,0002,433,024
21Mar 20, 2026 19:362,51560,231,27329,635,0003,065,0002,506,752
22Mar 20, 2026 19:362,53062,561,96629,906,0002,990,0002,424,832
23Mar 20, 2026 19:372,54659,122,84527,083,0006,018,0002,387,968
24Mar 20, 2026 19:482,56063,471,87130,256,0003,025,0002,289,664
25Mar 20, 2026 19:372,58560,513,03828,513,0005,091,0002,420,736
26Mar 20, 2026 19:482,60760,717,97826,919,0006,978,0002,351,104
27Mar 20, 2026 19:362,61565,534,66831,992,0001,999,0002,449,408
28Mar 20, 2026 19:362,70055,119,46632,088,0003,008,0002,465,792
29Mar 20, 2026 19:362,75964,215,59832,958,0002,908,0002,260,992
30Mar 20, 2026 19:362,76777,306,34629,972,0005,994,0002,269,184