Compute challenges

Sergey Svistunov24 Apr 2021
30s 512Mb 1

Compute the sum of integers read from STDIN as fast as possible.

Input: 50 000 000 lines, each containing one integer in the range [0; 2147483647].

629871117
2024562523
1372689083
1021777120
2111176472

Output: The uint64 sum of all numbers, printed as a decimal string.

Note: Integer overflow is expected – use a 64-bit accumulator.

Sergey Svistunov26 Apr 2021
20s 512Mb 1

Count the exact number of unique tokens as fast as possible.

Input

One token per line on STDIN:

JWXcKKaWzvFL5
Rof
3ztCpA
5wHcGN
*UiEMthaTS*g

Output

Print the exact count of unique tokens to STDOUT.

Constraints

  • Character set: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@#%*
  • Maximum token length: 16
  • Approximately 1,000,000 unique tokens
Sergey Svistunov30 Apr 2021
60s 512Mb 1

Compute the total amount of non-canceled external USD transactions from a stream of JSON records as fast as possible.

Input: 1 000 000 JSON records on STDIN, one per line. Each record has the following structure:

{
    "user_id": 0,
    "currency": "USD",
    "transactions": [
        {
            "amount": 0,
            "to_user_id": 0,
            "canceled": false
        }
    ]
}
Field Constraints
user_id Integer, max 10 000
currency One of "GBP", "USD", "RUB", "JPY", "CHF"
transactions Array, max 10 elements
amount Integer, max 1000
canceled Boolean; may be omitted when false

Field order is not guaranteed within any object.

Output: The total amount of all transactions where:

  • record.currency == "USD"
  • transaction.to_user_id != record.user_id (external)
  • transaction.canceled is false or absent
Sergey Svistunov03 May 2021
60s 256Mb 1

Compute a checksum over the decimal representations of binary integers as fast as possible.

Input: 250 000 000 uint32 values in little-endian binary on STDIN (4 bytes each).

Output: A uint64 checksum computed as:

CRC = sum of number_crc(n) for each n

where number_crc(n) converts n to its decimal string and sums ascii(digit) * position over each digit (0-indexed from the left).

Example: For n = 42, the decimal string is "42", so number_crc(42) = ascii('4') * 0 + ascii('2') * 1 = 52 * 0 + 50 * 1 = 50.

Sergey Svistunov11 May 2021
30s 512Mb 1

Count the number of bytes equal to 127 in a binary stream as fast as possible.

Input: 250 000 000 uint8 values in binary on STDIN.

Output: The count of elements equal to 127, printed as a decimal string.

Sergey Svistunov25 May 2021
60s 512Mb 1

Convert 1,000,000 person records from XML to JSON as fast as possible.

Input

XML document on STDIN:

<?xml version="1.0" encoding="UTF-8"?>
<persons>
    <person id="1512376">
        <age>30</age>
        <height>169.1</height>
        <married>true</married>
        <phone code="+6"><number>1283603279</number></phone>
        <phone code="+6"><number>1659964668</number></phone>
    </person>
    ...
</persons>

Output

One JSON object per person to STDOUT, preserving order:

{
    "id": 1512376,
    "age": 30,
    "height": 169.1,
    "married": true,
    "phones": [
        {
            "code": "+6",
            "number": 1283603279
        },
        {
            "code": "+6",
            "number": 1659964668
        }
    ]
}

Constraints

  • Preserve the order of persons
  • Omit the phones field if the phone array is empty
  • Maximum number of phones per person is 3
Sergey Svistunov06 Jun 2021
20s 512Mb 3

Same problem as Unique strings, but with 3 CPUs available and scoring based on wall time instead of CPU time.

Count the exact number of unique tokens as fast as possible.

Input

One token per line on STDIN:

JWXcKKaWzvFL5
Rof
3ztCpA
5wHcGN
*UiEMthaTS*g

Output

Print the exact count of unique tokens to STDOUT.

Constraints

  • Character set: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@#%*
  • Maximum token length: 16
  • Approximately 1,000,000 unique tokens
Sergey Svistunov11 Jun 2021
20s 512Mb 1

Find the median of a stream of binary integers as fast as possible.

Input: 100 000 000 uint32 values in little-endian binary on STDIN (4 bytes each).

Output: The median value, printed as a decimal string. The median is defined as the element at index N/2 (i.e., a[50000000]) in the sorted array.

Mikhail Shirokov15 Jun 2021
90s 256Mb 1

Sort 18,000,000 UUIDs as fast as possible.

Input

One UUID per line on STDIN in standard format:

01c39d6f-a905-41b5-b960-55f5be445bf8
790455cb-9813-4298-87ec-9ede1dc38e10
8ff38a09-f995-467b-9d07-5536f621402e
...

Output

All UUIDs sorted in ascending lexicographic order, one per line, to STDOUT.

30s 256Mb 1

Compute the sum of all prime numbers in a stream of binary integers as fast as possible.

Input: 1 000 000 uint32 values in little-endian binary on STDIN (4 bytes each).

Output: The uint64 sum of all values that are prime, printed as a decimal string.

TopK-RP Daily
Sergey Svistunov07 Jul 2021
60s 512Mb 1

Find the sum of the 100 largest values in a stream of binary integers as fast as possible.

Input: 100 000 000 uint32 values in little-endian binary on STDIN (4 bytes each).

Output: The uint64 sum of the top 100 greatest values, printed as a decimal string.

30s 256Mb 1

Evaluate 100 arithmetic expressions as fast as possible.

Input

One expression per line on STDIN. Each expression contains 50,000 int16 numbers with operators +, -, *, / (integer division) and parentheses (, ).

-19550 - ((-14208 / (13583 + -19215)) + (-16832 / 797 + (9060 / -23627)) + ((-6060) + 24953))
30835 - 3703 - (-20089 * -6261 + ((-28985 - 29627) + (-17828 - (22773 / -4014) * 1630)))

Output

Print the int64 result of each expression, one per line, to STDOUT.

Sergey Svistunov20 Sep 2021
30s 512Mb 3

Compute the MD5 hash of a binary blob as fast as possible. 3 CPU cores are available.

Input: 250 000 000 bytes on STDIN.

Output: The MD5 digest as a lowercase hexadecimal string (32 characters).

Andrey Tsvetkov09 Oct 2021
30s 256Mb 1

Compute the sum of 5,000,000 UTC Unix timestamps as fast as possible.

Input

One RFC 3339 datetime per line on STDIN:

2017-05-04T14:31:30-03:00
2046-06-23T11:51:56-06:00
2031-08-14T13:18:38+06:00
2048-04-14T05:55:06-09:00
1980-08-28T00:43:03+02:00

Output

Print the sum of all UTC Unix timestamps (int64) to STDOUT.

Constraints

  • Datetimes are in the range 1950-01-01T00:00:00 to 2050-12-31T23:59:59
Yuriy Lyfenko21 Nov 2021
30s 256Mb 1

Simulate a sell-side order book and calculate the cost of a final purchase as fast as possible.

Input

1,000,000 order updates on STDIN, one per line:

  • + <price> <size> – add a new sell order
  • - <position> – delete the order at the given position
  • = <size> – buy <size> shares from the top of the order book

After all updates are processed, buy 1,000 shares from the top of the order book.

Output

Print the total cost of the final 1,000-share purchase to STDOUT.

Order Book Rules

Orders are sorted by price ascending (lower is better). Orders at the same price are sorted by arrival time (earlier first). Position 0 is the best (lowest-price) offer.

The = (buy) operation consumes shares starting from position 0. If an order is fully consumed, it is removed from the book.

Example

Input Order book state
+ 1137 100 (1137,100)
+ 1130 10 (1130,10), (1137,100)
+ 1130 50 (1130,10), (1130,50), (1137,100)
- 0 (1130,50), (1137,100)
+ 1150 200 (1130,50), (1137,100), (1150,200)
= 200 (1150,150)

Total cost of the last buy: 50 * 1130 + 100 * 1137 + 50 * 1150.

Sergey Svistunov26 Nov 2022
30s 256Mb 1

Perform FizzBuzz on a stream of binary integers as fast as possible.

Input: 30 000 000 uint32 values in little-endian binary on STDIN (4 bytes each).

Output: For each number n, print one line:

  • FizzBuzz if n is divisible by both 3 and 5
  • Fizz if n is divisible by 3
  • Buzz if n is divisible by 5
  • n as a decimal string otherwise
30s 256Mb 1

Extract the Blue channel from a stream of RGBA pixels as fast as possible.

Input: 125 000 000 pixels in binary RGBA format on STDIN (4 bytes per pixel: R, G, B, A).

Output: Write only the Blue byte of each pixel to STDOUT (125 000 000 bytes total).

60s 256Mb 1

Multiply two square matrices as fast as possible.

Input

Two 2000 x 2000 matrices of uint32 values on STDIN, encoded back-to-back in row-major order, little-endian byte order.

Output

Write the resulting 2000 x 2000 product matrix to STDOUT in the same binary format (row-major, little-endian uint32).

30s 256Mb 1

Extract the Blue channel from a stream of RGB pixels as fast as possible.

Input: 150 000 000 pixels in binary RGB format on STDIN (3 bytes per pixel: R, G, B).

Output: Write only the Blue byte of each pixel to STDOUT (150 000 000 bytes total).

600s 256Mb 1

Multiply two large unsigned integers as fast as possible.

Input

Exactly 500,000 bytes on STDIN: two unsigned integers encoded back-to-back, each 250,000 bytes, in little-endian byte order.

Output

Write exactly 500,000 bytes to STDOUT containing the product in little-endian byte order.

60s 256Mb 1

Find the largest square submatrix consisting entirely of 1s as fast as possible.

Input

A 10,000 x 10,000 matrix of uint8 values (0 or 1) on STDIN.

0, 1, 0, 1, 1
0, 1, 1, 1, 1
0, 0, 1, 0, 0
1, 0, 1, 1, 0

Output

Print the side length of the largest all-ones square submatrix to STDOUT. The answer is guaranteed to be greater than 1.

For the example above, the output is:

2
90s 256Mb 1

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