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 26, 2025 22:53Josu San MartinJosu San MartinScore: 6,081Success
Source Code

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

Challenge History

No challenges yet.

Run Statistics
#DateScoreWall TimeCPU UserCPU SystemMemoryError
1Dec 26, 2025 22:525,10492,831,72762,325,0004,021,0002,293,760
2Dec 26, 2025 22:525,22295,135,66863,898,0003,993,0002,351,104
3Dec 26, 2025 22:525,450105,110,15967,855,0002,993,0002,490,368
4Dec 26, 2025 22:525,452101,189,15762,885,0007,985,0002,424,832
5Dec 26, 2025 22:515,604103,072,68868,865,0003,992,0002,457,600
6Dec 26, 2025 22:515,60498,971,57168,857,0003,991,0002,482,176
7Dec 26, 2025 22:535,627100,977,58564,130,0009,018,0002,326,528
8Dec 26, 2025 22:525,681104,756,58269,858,0003,991,0002,408,448
9Dec 26, 2025 22:525,709102,174,43872,207,0002,005,0002,412,544
10Dec 26, 2025 22:525,738106,764,84872,573,0002,015,0002,383,872
11Dec 26, 2025 22:525,757101,093,43771,845,0002,993,0002,469,888
12Dec 26, 2025 22:535,81299,582,70473,537,0002,014,0002,383,872
13Dec 26, 2025 22:535,834103,268,45772,815,0003,033,0002,387,968
14Dec 26, 2025 22:525,840104,425,57870,926,0004,994,0002,355,200
15Dec 26, 2025 22:525,929102,657,77771,075,0006,006,0002,375,680
16Dec 26, 2025 22:535,936100,702,55973,163,0004,008,0002,527,232
17Dec 26, 2025 22:535,947102,830,93473,293,0004,016,0002,330,624
18Dec 26, 2025 22:525,970105,711,65374,592,0003,024,0002,301,952
19Dec 26, 2025 22:535,991104,804,59470,889,0006,989,0002,273,280
20Dec 26, 2025 22:526,012104,448,02572,148,0006,012,0002,371,584
21Dec 26, 2025 22:536,020106,413,97674,245,0004,013,0002,510,848
22Dec 26, 2025 22:536,081104,118,79775,052,0004,002,0002,453,504
23Dec 26, 2025 22:526,110103,509,01776,420,0003,016,0002,469,888
24Dec 26, 2025 22:526,142112,512,32375,857,0003,992,0002,379,776
25Dec 26, 2025 22:526,154106,645,32673,999,0005,999,0002,392,064
26Dec 26, 2025 22:526,172103,389,41175,225,0005,015,0002,408,448
27Dec 26, 2025 22:526,201109,748,75277,594,0003,023,0002,371,584
28Dec 26, 2025 22:526,201109,341,23078,599,0002,015,0002,478,080
29Dec 26, 2025 22:526,205108,666,79378,646,0002,016,0002,404,352
30Dec 26, 2025 22:526,205108,097,26477,644,0003,025,0002,371,584
31Dec 26, 2025 22:516,240104,680,37878,118,0003,004,0002,371,584
32Dec 26, 2025 22:526,264109,320,43877,413,0004,021,0002,478,080
33Dec 26, 2025 22:526,268113,615,85276,459,0005,030,0002,457,600
34Dec 26, 2025 22:536,376111,365,27577,891,0004,993,0002,355,200
35Dec 26, 2025 22:526,533114,439,80979,931,0004,995,0002,371,584
36Dec 26, 2025 22:536,635113,481,16482,243,0004,011,0002,306,048
37Dec 26, 2025 22:526,653108,476,47583,470,0003,016,0002,441,216
38Dec 26, 2025 22:536,744119,634,42581,623,0006,046,0002,351,104
39Dec 26, 2025 22:526,769117,922,78885,999,0001,999,0002,347,008
40Dec 26, 2025 22:526,868117,457,61788,278,0001,003,0002,428,928
41Dec 26, 2025 22:527,221120,558,44286,880,0006,990,0002,322,432
42Dec 26, 2025 22:537,321117,634,45791,168,0004,007,0002,289,664