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 listDec 29, 2025 00:05Josu San MartinJosu San MartinScore: 5,247Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 29, 2025 00:064,08178,916,32050,056,0003,003,0002,334,720
2Dec 29, 2025 00:084,11779,716,79151,507,0002,019,0002,281,472
3Dec 29, 2025 00:084,24782,707,43849,184,0006,022,0002,420,736
4Dec 29, 2025 00:064,37882,674,43151,922,0004,992,0002,289,664
5Dec 29, 2025 00:054,42786,355,72954,518,0003,028,0002,277,376
6Dec 29, 2025 00:064,45290,709,87154,885,0002,993,0002,510,848
7Dec 29, 2025 00:084,49985,862,34055,465,0003,025,0002,379,776
8Dec 29, 2025 00:084,57295,755,56356,416,0003,022,0002,473,984
9Dec 29, 2025 00:054,66593,006,79558,618,0002,021,0002,457,600
10Dec 29, 2025 00:054,69092,697,45257,975,0002,998,0002,277,376
11Dec 29, 2025 00:054,75988,388,58457,880,0003,991,0002,355,200
12Dec 29, 2025 00:064,76490,899,61056,935,0004,994,0002,355,200
13Dec 29, 2025 00:084,83689,784,84460,871,0001,995,0002,469,888
14Dec 29, 2025 00:064,85291,211,78459,071,0004,004,0002,301,952
15Dec 29, 2025 00:065,00594,819,33660,066,0005,005,0002,273,280
16Dec 29, 2025 00:065,01289,210,12161,152,0004,009,0002,289,664
17Dec 29, 2025 00:095,02290,844,72862,276,0003,013,0002,486,272
18Dec 29, 2025 00:065,05092,543,73762,621,0003,030,0002,314,240
19Dec 29, 2025 00:055,10193,705,59461,291,0005,023,0002,445,312
20Dec 29, 2025 00:065,21997,730,90863,857,0003,991,0002,416,640
21Dec 29, 2025 00:065,24494,865,88463,157,0005,012,0002,355,200
22Dec 29, 2025 00:295,24793,143,11465,206,0003,009,0002,416,640
23Dec 29, 2025 00:065,27699,631,17568,592,00002,498,560
24Dec 29, 2025 00:065,28395,745,26264,641,0004,040,0002,293,760
25Dec 29, 2025 00:065,30399,547,92367,935,000999,0002,367,488
26Dec 29, 2025 00:065,33698,148,75865,346,0004,021,0002,342,912
27Dec 29, 2025 00:295,375100,084,67666,878,0002,994,0002,363,392
28Dec 29, 2025 00:295,39195,563,35068,085,0002,002,0002,404,352
29Dec 29, 2025 00:065,44998,524,57767,844,0002,993,0002,269,184
30Dec 29, 2025 00:095,53199,625,61665,915,0005,992,0002,441,216
31Dec 29, 2025 00:055,581103,327,76869,531,0003,023,0002,428,928
32Dec 29, 2025 00:095,62399,258,68270,096,0003,004,0002,510,848
33Dec 29, 2025 00:055,641102,848,17068,315,0005,023,0002,510,848
34Dec 29, 2025 00:085,738103,603,80774,599,00002,482,176
35Dec 29, 2025 00:065,77098,800,46473,009,0002,000,0002,318,336
36Dec 29, 2025 00:085,845103,933,11672,983,0002,999,0002,482,176
37Dec 29, 2025 00:086,078109,297,58276,018,0003,000,0002,371,584
38Dec 29, 2025 00:056,100106,321,77476,292,0003,011,0002,355,200
39Dec 29, 2025 00:056,142107,355,25776,851,0002,994,0002,424,832
40Dec 29, 2025 00:066,173108,592,91373,231,0007,022,0002,342,912
41Dec 29, 2025 00:066,177106,672,92375,281,0005,018,0002,465,792
42Dec 29, 2025 00:086,397111,559,66479,155,0004,007,0002,506,752