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
#AuthorScoreLanguageSolution
1Andrew EpsteinAndrew Epstein Master2,081
CPP
g++14.2.0
Aug 23, 2026 03:37
37,005,719 ns · 24,794,000 ns · 2,262,000 ns · 2,113,536 B
2Victor MerckléVictor Mercklé Grandmaster2,102
CPP
g++14.2.0
Sep 1, 2026 06:09
35,618,315 ns · 26,305,000 ns · 1,028,000 ns · 2,117,632 B
3Josu San MartinJosu San Martin Expert2,291
RUST
rust-1.98.1
Sep 4, 2026 19:00
43,361,146 ns · 27,725,000 ns · 2,068,000 ns · 2,117,632 B
4Yuriy LyfenkoYuriy Lyfenko Expert2,296
CPP
clang++18.1.3
Mar 21, 2026 19:53
60,483,252 ns · 25,870,000 ns · 3,980,000 ns · 2,363,392 B
5Tomislav TunkovicTomislav Tunkovic Grandmaster2,370
GO
go1.26.2
Aug 16, 2026 22:59
39,181,064 ns · 25,930,000 ns · 4,882,000 ns · 2,138,112 B
6Zhou180Zhou180 Apprentice2,851
CPP
g++10.5.0
Jun 21, 2026 10:24
46,797,336 ns · 33,917,000 ns · 3,152,000 ns · 2,113,536 B
7marko1597marko1597 Expert2,865
CSHARP
9.0.15
Aug 17, 2026 00:53
46,924,394 ns · 29,946,000 ns · 7,305,000 ns · 2,154,496 B
8yenw0dyenw0d Novice3,176
RUST
rust-1.85.1
Dec 30, 2025 20:30
66,103,010 ns · 38,272,000 ns · 3,021,000 ns · 2,301,952 B
9highloadLLMhighloadLLM Apprentice3,383
CPP
clang++20.1.2
Jun 7, 2026 02:07
53,168,100 ns · 41,897,000 ns · 2,083,000 ns · 2,117,632 B
10olliecrowolliecrow Expert5,022
RUST
rust-1.94.0
May 6, 2026 10:06
79,443,817 ns · 62,161,000 ns · 3,128,000 ns · 2,478,080 B
11M-RochatM-Rochat Expert5,221
CPP
clang++17.0.6
May 22, 2026 22:06
79,384,487 ns · 60,037,000 ns · 7,848,000 ns · 2,113,536 B
12Dominique GarmierDominique Garmier Expert13,849
CPP
clang++20.1.2
May 1, 2026 09:34
196,188,069 ns · 176,045,000 ns · 4,001,000 ns · 2,310,144 B
13KeefeKeefe Expert14,809
CPP
g++13.3.0
Mar 13, 2026 19:40
218,099,332 ns · 185,499,000 ns · 7,018,000 ns · 2,293,760 B
14imotw2imotw2 Novice15,043
CPP
g++13.3.0
Oct 13, 2025 16:28
218,489,487 ns · 190,549,000 ns · 5,014,000 ns · 2,416,640 B
15anboto1anboto1 Novice19,641
CPP
g++14.2.0
Jun 7, 2026 01:49
264,794,842 ns · 251,075,000 ns · 4,269,000 ns · 2,113,536 B
16Carl BergmanCarl Bergman Novice24,109
RUST
rust-1.90.0
Mar 15, 2026 03:15
336,833,010 ns · 307,404,000 ns · 6,007,000 ns · 2,359,296 B
17Donald LeeDonald Lee Apprentice30,266
RUST
rust-1.95.0
Apr 23, 2026 20:25
411,863,628 ns · 386,480,000 ns · 6,990,000 ns · 2,506,752 B
18Charles CabergsCharles Cabergs Skilled35,220
CPP
g++13.3.0
Apr 30, 2026 12:14
476,809,312 ns · 448,870,000 ns · 8,997,000 ns · 2,514,944 B
19lipapipalipapipa Skilled39,187
CPP
g++13.3.0
Feb 7, 2026 19:27
540,301,755 ns · 503,428,000 ns · 6,005,000 ns · 2,424,832 B
20idatsyidatsy Novice40,753
RUST
rust-1.92.0
Dec 20, 2025 12:07
553,024,924 ns · 515,801,000 ns · 13,994,000 ns · 2,531,328 B
21Aniruddha DebAniruddha Deb Expert47,419
CPP
g++13.3.0
Dec 31, 2025 15:22
639,540,418 ns · 615,446,000 ns · 1,000,000 ns · 348,160 B
22JarnoJarno Novice68,724
RUST
rust-1.94.0
Mar 12, 2026 16:36
919,163,741 ns · 890,420,000 ns · 2,998,000 ns · 2,469,888 B
23izotoffizotoff Skilled70,444
GO
go1.25.4
Nov 26, 2025 12:35
943,382,828 ns · 913,768,000 ns · 1,999,000 ns · 2,281,472 B
24WilmerFinleyWilmerFinley Novice98,971
CPP
g++13.3.0
Oct 13, 2025 06:43
1,313,910,752 ns · 1,225,642,000 ns · 60,982,000 ns · 2,486,272 B
25A_ShamanA_Shaman Skilled100,773
CPP
g++10.5.0
Jun 20, 2026 11:06
1,319,561,845 ns · 1,302,138,000 ns · 7,920,000 ns · 2,113,536 B
26BenBen Skilled115,528
CPP
g++14.2.0
Apr 20, 2026 19:09
1,530,628,736 ns · 1,491,882,000 ns · 9,992,000 ns · 2,342,912 B
27regnivonregnivon Novice131,491
RUST
rust-1.92.0
Dec 28, 2025 00:56
1,744,475,347 ns · 1,699,381,000 ns · 9,996,000 ns · 2,523,136 B
28Ludvig SjöströmLudvig Sjöström Novice167,102
CPP
g++13.3.0
Oct 11, 2025 22:45
2,201,763,382 ns · 2,157,335,000 ns · 14,988,000 ns · 2,473,984 B
29TudyMTudyM Skilled168,151
CPP
clang++20.1.2
Apr 27, 2026 22:00
2,201,811,631 ns · 2,170,974,000 ns · 14,992,000 ns · 2,412,544 B
30Oleg KovalovOleg Kovalov Skilled205,816
RUST
rust-1.90.0
Oct 25, 2025 10:50
2,701,179,433 ns · 2,667,617,000 ns · 7,995,000 ns · 2,453,504 B
31Haha ExpertHaha Expert Expert208,689
RUST
rust-1.93.1
Mar 3, 2026 13:26
2,746,965,552 ns · 2,699,967,000 ns · 12,995,000 ns · 2,416,640 B
32Daniel HarrisDaniel Harris Novice212,546
RUST
rust-1.90.0
Oct 5, 2025 16:52
2,791,406,829 ns · 2,756,108,000 ns · 6,995,000 ns · 2,285,568 B
33Enda CarrollEnda Carroll Novice233,148
RUST
rust-1.90.0
Oct 12, 2025 11:50
3,060,465,804 ns · 3,019,937,000 ns · 10,992,000 ns · 2,347,008 B
34Yehor BondarYehor Bondar Skilled266,847
CPP
g++14.2.0
Aug 13, 2026 10:09
3,479,106,411 ns · 3,458,892,000 ns · 10,127,000 ns · 2,117,632 B
35Taylor DettlingTaylor Dettling Skilled268,197
RUST
rust-1.95.0
Apr 21, 2026 01:30
3,508,359,224 ns · 3,472,580,000 ns · 13,990,000 ns · 2,375,680 B
36RayRay Novice276,287
CPP
g++10.5.0
Jul 11, 2026 04:09
3,601,556,207 ns · 3,581,699,000 ns · 10,038,000 ns · 2,113,536 B
37TonyTony Skilled279,162
CPP
g++10.5.0
Jul 31, 2026 06:16
3,639,363,738 ns · 3,618,814,000 ns · 10,296,000 ns · 2,117,632 B
38Genos0Genos0 Novice309,155
CPP
g++10.5.0
Jul 10, 2026 18:40
4,029,541,262 ns · 4,007,795,000 ns · 11,221,000 ns · 2,113,536 B