Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

Tick-Indexed Price Ladder

Prerequisites: STL Container Choice, map, list, and deque Trade-Offs, Cache Lines, Locality, and Pointer Chasing

A market data handler keeps, for each symbol such as AAPL, a summary of the orders resting at the exchange. Build this two-sided summary, a price ladder, for one symbol.

  • Bids and asks: orders to buy and orders to sell.
  • Price level: all the orders on one side at one price. Only its total quantity is stored, not the individual orders.
  • Best bid: the highest price with a non-zero bid quantity.
  • Best ask: the lowest price with a non-zero ask quantity.
  • Spread: the gap between the best bid and the best ask.
  • Tick size: the fixed step prices move in, so the valid prices form a grid.

What to implement

  • process_commands(base, tick_size, levels, commands) returns the printed lines in command order.
  • Each command is one line, and side is BID or ASK.
  • On stdin, the first three lines are base, tick_size, and levels. The fourth is the number of commands.
  • There are three parts, each adding behavior to the previous one. One solution must pass all eleven tests.
CommandMeaningPrints
SET side price qtySet the total quantity at price to qty; 0 empties the levelnothing (Part 2: OUT_OF_BAND)
ADD side price deltaAdd the signed delta to the quantity at pricenothing (Part 2: OUT_OF_BAND)
BESTBest level on each sideBID level ASK level
DEPTH side kBest k non-empty levels on sideside level level ...
RECENTER new_baseMove the band (Part 3)DROPPED b a

A level prints as price:quantity, for example 19010:300. A side with no non-empty level prints NONE in its place.

The band

STL Container Choice noted that when keys are dense small integers, an array indexed by the key beats a map. Subtracting a base price and dividing by the tick size turns prices near the market into such keys.

  • The ladder covers a band of levels consecutive grid prices starting at base. Level i holds the price base + i * tick_size, so the level of a price is (price - base) / tick_size.
  • Each side is an array of levels quantities with a cached best index, so reading the best bid and best ask needs no search.
  • A std::map from price to quantity per side also produces the right output, and the tests check only the output. The Approach compares the two layouts.

Part 1: Levels and the cached best

  1. SET replaces the quantity at a level: SET BID 19010 300 then SET BID 19010 500 leaves 500, not 800.
  2. ADD changes the quantity by delta. A level whose quantity reaches 0 is empty.
  3. A SET or ADD that leaves an empty level at 0 changes nothing.
  4. The two sides are stored separately. A command on one side never changes the other.
  5. Each side's best index stays cached and is updated as levels change:
    • a level that becomes non-empty closer to the spread than the cached best becomes the new best;
    • a change to any other level leaves the best where it is;
    • only when the best level itself becomes empty does the ladder scan outward, away from the spread, to the next non-empty level, or to the end of the array if there is none.

Output

BID level ASK level
side level level ...
  • BEST: for example BID 19010:300 ASK 19011:200, or BID NONE ASK 19011:200 when every bid level is empty.
  • DEPTH side k: side, then the side's first k non-empty levels from the best. Bids go from the highest price down, asks from the lowest price up.
  • Empty levels are skipped and do not count toward k. A side with fewer than k non-empty levels prints all of them; a side with none prints side NONE.

Limits and guarantees

  • levels and tick_size are between 1 and 10,000. There are at most 10,000 commands.
  • base, every new_base, and every price are between 1 and 1012. That does not fit in a 32-bit int, so store prices in std::int64_t.
  • Every price and every new_base lies on the tick grid: it differs from the initial base by a whole multiple of tick_size.
  • qty is between 0 and 109. delta is between -109 and 109 and may be 0.
  • An ADD never takes a level's quantity below 0. Many ADDs can still push a level past 231, so store quantities in std::int64_t as well.
  • k is between 1 and levels.
  • The book is never crossed: whenever both sides have a non-empty level, the best bid price is below the best ask price.

Tests 1–4 cover this part. Every price in them lies inside the band.

Part 2: Prices outside the band

Prices can now fall below base or above base + (levels - 1) * tick_size. Both of those end prices are inside the band.

  1. A SET or ADD whose price is outside the band prints OUT_OF_BAND and changes nothing, whatever its quantity or delta. That includes a SET with quantity 0.
  2. BEST and DEPTH work as before.

Tests 5–7 cover this part. Test 6 has a band of a single level.

Part 3: Recentering the band

When the market drifts toward the edge of the band, the handler moves the band with it.

  1. RECENTER new_base makes new_base the price of level 0. The band keeps levels levels and the same tick_size, so it covers new_base through new_base + (levels - 1) * tick_size.
  2. new_base may be above, below, or equal to the current base, and may move the band by levels levels or more.
  3. A non-empty level whose price is inside the new band keeps its quantity at the same price. Only its index changes, to (price - new_base) / tick_size.
  4. A level whose price is outside the new band is dropped, on both sides. Its quantity does not come back if a later RECENTER covers that price again.
  5. Prices that the new band covers and the old band did not start empty.
  6. After the move, each side's best is its best remaining non-empty level, or NONE.
  7. Every later command uses the new band, including the Part 2 band check.

Output: DROPPED b a, where b and a are the numbers of non-empty bid and ask levels discarded. Empty levels that fall outside the band are not counted.

Tests 8–11 cover this part.

Example
Input
19000
5
8
9
SET BID 19010 300
SET BID 19000 100
SET ASK 19020 200
SET ASK 19030 400
BEST
DEPTH ASK 3
ADD BID 19010 -300
BEST
DEPTH BID 2
Output
BID 19010:300 ASK 19020:200
ASK 19020:200 19030:400
BID 19000:100 ASK 19020:200
BID 19000:100
Explanation

Level i holds the price 19000 + 5i, so the band runs from 19000 to 19035.

The bid at 19010 sits at level (19010 - 19000) / 5 = 2 and becomes the best bid. The bid at 19000 lands at level 0, further from the spread, so the best stays at level 2.

DEPTH ASK 3 finds only two non-empty ask levels and prints both.

ADD BID 19010 -300 empties the best bid level, so the ladder scans downward. Level 1 (19005) is empty and level 0 (19000) holds 100, which becomes the new best bid.