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 21, 2026 19:54Yuriy LyfenkoYuriy LyfenkoScore: 2,301Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Mar 21, 2026 20:032,06953,025,83020,919,0005,977,0002,408,448
2Mar 21, 2026 19:492,18054,771,76524,295,0004,049,0002,433,024
3Mar 21, 2026 19:492,18054,129,54626,322,0002,024,0002,363,392
4Mar 21, 2026 20:032,18154,343,39125,316,0003,037,0002,445,312
5Mar 21, 2026 19:532,19656,575,15224,469,0004,078,0002,297,856
6Mar 21, 2026 19:532,21851,860,08426,841,0001,988,0002,285,568
7Mar 21, 2026 19:492,22552,988,76922,937,0005,983,0002,334,720
8Mar 21, 2026 19:542,22553,037,33023,936,0004,986,0002,322,432
9Mar 21, 2026 19:542,22657,395,99723,954,0004,990,0002,502,656
10Mar 21, 2026 19:492,24453,687,61526,157,0003,018,0002,478,080
11Mar 21, 2026 19:492,24955,365,24523,192,0006,050,0002,535,424
12Mar 21, 2026 20:032,25557,975,53624,261,0005,054,0002,523,136
13Mar 21, 2026 20:032,25758,637,36025,295,0004,047,0002,359,296
14Mar 21, 2026 20:062,26249,120,93624,333,0005,069,0002,269,184
15Mar 21, 2026 20:032,27463,376,39124,467,0005,097,0002,531,328
16Mar 21, 2026 19:542,27756,977,54224,503,0005,104,0002,539,520
17Mar 21, 2026 19:492,27751,628,91224,501,0005,104,0002,490,368
18Mar 21, 2026 20:062,28460,396,39524,577,0005,120,0002,338,816
19Mar 21, 2026 20:032,29556,852,70724,864,0004,972,0002,375,680
20Mar 21, 2026 20:062,30154,377,62325,926,0003,988,0002,367,488
21Mar 21, 2026 19:532,31354,827,96927,068,0003,007,0002,375,680
22Mar 21, 2026 19:492,31957,934,07026,126,0004,019,0002,482,176
23Mar 21, 2026 20:032,32954,824,56327,251,0003,027,0002,510,848
24Mar 21, 2026 20:062,33862,211,67828,373,0002,026,0002,523,136
25Mar 21, 2026 19:492,33957,250,47626,356,0004,054,0002,400,256
26Mar 21, 2026 19:492,34958,029,25126,465,0004,071,0002,404,352
27Mar 21, 2026 20:032,37456,560,57725,882,0004,977,0002,322,432
28Mar 21, 2026 19:492,38562,455,50429,003,0002,000,0002,433,024
29Mar 21, 2026 19:492,39458,599,94127,102,0004,015,0002,433,024
30Mar 21, 2026 20:032,40255,362,74526,188,0005,036,0002,367,488
31Mar 21, 2026 20:062,42957,570,77925,469,0006,112,0002,449,408
32Mar 21, 2026 19:492,43667,678,33829,554,0002,111,0002,457,600
33Mar 21, 2026 19:492,45456,001,74427,914,0003,987,0002,449,408
34Mar 21, 2026 19:542,46264,452,60427,001,0005,000,0002,498,560
35Mar 21, 2026 19:542,46356,960,88829,021,0003,002,0002,281,472
36Mar 21, 2026 19:492,47861,423,37926,178,0006,041,0002,277,376
37Mar 21, 2026 20:062,48166,474,36429,227,0003,023,0002,396,160
38Mar 21, 2026 19:492,52164,827,39527,812,0004,966,0002,392,064
39Mar 21, 2026 19:542,63064,325,82031,179,0003,017,0002,404,352