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 12, 2025 19:38imotw2imotw2Score: 44,414Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Oct 12, 2025 18:4934,169469,599,351439,191,0005,002,0002,437,120
2Oct 12, 2025 18:0536,006495,835,406466,077,0002,000,0002,375,680
3Oct 12, 2025 18:4936,841506,365,002474,928,0003,999,0002,449,408
4Oct 12, 2025 18:3236,936510,574,131477,172,0003,001,0002,371,584
5Oct 12, 2025 18:4937,967518,808,483489,572,0003,996,0002,498,560
6Oct 12, 2025 18:4938,647526,632,188499,413,0003,002,0002,478,080
7Oct 12, 2025 18:4839,187538,421,862507,435,0001,997,0002,441,216
8Oct 12, 2025 18:4940,375552,160,053521,878,0002,999,0002,453,504
9Oct 12, 2025 19:3540,531553,057,499521,903,0004,999,0002,289,664
10Oct 12, 2025 18:3240,571555,630,318524,421,0002,996,0002,437,120
11Oct 12, 2025 18:0541,079567,557,794528,029,0006,000,0002,404,352
12Oct 12, 2025 18:0541,135561,516,394530,755,0003,998,0002,301,952
13Oct 12, 2025 18:0542,076570,015,385544,990,0001,999,0002,494,464
14Oct 12, 2025 18:4942,261573,476,342546,395,0003,002,0002,269,184
15Oct 12, 2025 18:0542,371581,060,385547,826,0002,999,0002,445,312
16Oct 12, 2025 18:0542,585578,507,716549,611,0003,997,0002,445,312
17Oct 12, 2025 18:3442,965587,349,103555,552,0002,997,0002,433,024
18Oct 12, 2025 18:0543,081588,319,661558,050,0002,000,0002,334,720
19Oct 12, 2025 18:4943,251590,984,458558,269,0003,994,0002,465,792
20Oct 12, 2025 19:3543,698594,570,728567,069,0001,000,0002,416,640
21Oct 12, 2025 18:4743,790595,344,706566,268,0002,996,0002,306,048
22Oct 12, 2025 19:1843,824595,724,932566,713,0002,998,0002,445,312
23Oct 12, 2025 18:3344,414606,341,215575,389,0001,997,0002,473,984
24Oct 12, 2025 18:3444,524610,068,839576,816,0001,999,0002,445,312
25Oct 12, 2025 19:1844,532603,900,065574,915,0003,999,0002,387,968
26Oct 12, 2025 18:4844,690607,518,059577,974,0002,999,0002,297,856
27Oct 12, 2025 18:3344,697608,743,180581,057,00002,293,760
28Oct 12, 2025 18:0544,833610,576,132577,832,0004,998,0002,486,272
29Oct 12, 2025 18:4944,918612,839,800581,929,0001,999,0002,297,856
30Oct 12, 2025 18:5945,459616,348,202588,967,0001,999,0002,306,048
31Oct 12, 2025 19:3847,097640,545,470609,266,0003,001,0002,428,928
32Oct 12, 2025 18:0547,125638,491,941607,624,0004,996,0002,478,080
33Oct 12, 2025 18:4747,884648,783,914618,490,0003,996,0002,441,216
34Oct 12, 2025 18:5948,128653,554,338618,662,0006,996,0002,433,024
35Oct 12, 2025 18:3448,341662,084,822624,439,0003,996,0002,322,432
36Oct 12, 2025 19:1848,836663,087,392634,872,00002,428,928
37Oct 12, 2025 18:3249,067668,062,718634,868,0002,999,0002,408,448
38Oct 12, 2025 18:5949,118666,196,746634,534,0003,997,0002,424,832
39Oct 12, 2025 19:3849,656671,432,193640,532,0004,996,0002,387,968
40Oct 12, 2025 18:4850,123677,885,228645,600,0005,996,0002,478,080
41Oct 12, 2025 19:3850,487679,719,836653,333,0002,996,0002,424,832
42Oct 12, 2025 18:3351,343691,501,341663,464,0003,996,0002,457,600
43Oct 12, 2025 19:3552,118702,483,446672,537,0004,996,0002,453,504
44Oct 12, 2025 18:4953,237717,492,375689,081,0003,000,0002,416,640
45Oct 12, 2025 18:4754,497739,356,939704,468,0003,996,0002,420,736