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 listOct 9, 2025 15:31Yuriy LyfenkoYuriy LyfenkoScore: 6,104Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 9, 2025 15:314,68886,581,71255,952,0004,995,0002,514,944
2Oct 9, 2025 15:304,91394,330,03259,878,0003,991,0002,486,272
3Oct 9, 2025 15:315,34596,317,48166,467,0003,021,0002,326,528
4Oct 9, 2025 15:305,428100,153,53466,527,0004,031,0002,514,944
5Oct 9, 2025 15:305,43097,571,77866,553,0004,033,0002,494,464
6Oct 9, 2025 15:315,575105,610,54568,443,0004,026,0002,342,912
7Oct 9, 2025 15:315,581102,206,30468,527,0004,031,0002,490,368
8Oct 9, 2025 15:305,587109,753,96269,608,0003,026,0002,473,984
9Oct 9, 2025 15:315,703101,954,21471,132,0003,005,0002,387,968
10Oct 9, 2025 15:315,755103,446,28468,832,0005,985,0002,383,872
11Oct 9, 2025 15:305,880104,082,86472,420,0004,023,0002,297,856
12Oct 9, 2025 15:315,987110,658,18674,833,0002,993,0002,359,296
13Oct 9, 2025 15:306,023103,936,68173,277,0005,018,0002,424,832
14Oct 9, 2025 15:306,054108,291,03475,670,0003,026,0002,514,944
15Oct 9, 2025 15:316,102104,909,87573,298,0006,024,0002,433,024
16Oct 9, 2025 15:316,104109,044,65174,326,0005,022,0002,326,528
17Oct 9, 2025 15:316,248104,875,97877,219,0004,011,0002,338,816
18Oct 9, 2025 15:316,257107,105,10078,335,0003,012,0002,265,088
19Oct 9, 2025 15:316,279116,480,62278,604,0003,023,0002,523,136
20Oct 9, 2025 15:316,314104,888,18775,080,0007,007,0002,473,984
21Oct 9, 2025 15:316,352106,370,76077,543,0005,035,0002,498,560
22Oct 9, 2025 15:316,402117,576,75279,215,0004,010,0002,465,792
23Oct 9, 2025 15:306,450110,929,99978,865,0004,991,0002,506,752
24Oct 9, 2025 15:316,502109,810,74579,499,0005,031,0002,351,104
25Oct 9, 2025 15:306,610112,117,47380,939,0004,996,0002,486,272
26Oct 9, 2025 15:316,642112,369,68881,325,0005,020,0002,482,176
27Oct 9, 2025 15:316,789119,841,62684,240,0004,011,0002,486,272
28Oct 9, 2025 15:316,890116,152,50084,533,0005,031,0002,392,064
29Oct 9, 2025 15:317,211120,786,42288,760,0004,986,0002,514,944
30Oct 9, 2025 15:317,446123,439,85090,814,0005,987,0002,273,280