Market Data Toolkit
Prerequisites: Market Data to Strategy Handoff, Order Book API Shape
The hot core in Market Data to Strategy Handoff runs tick-to-trade and nothing else. Analytics, monitoring, and recovery checks run on a cold core, fed through a ring with data the feed handler has already normalized into one internal format.
Build four of those cold-path utilities behind a single entry point. Each must produce an exact answer defined by integer arithmetic, so that its report can be compared line for line with another system's.
What to implement
run_toolkit(lines)returns the mode's output lines.lines[0]names the mode. The remaining lines hold that mode's data, in the layout each part's Input section describes.- In the raw test input, the first line is the number of lines that follow. The harness reads it and passes the rest to
run_toolkitaslines. - Names of industries, tickers, ETFs, and stocks contain no whitespace, and no mode needs floating point.
- There are four parts, one mode each. One solution must pass all nineteen tests, so earlier modes must keep working as later ones are added.
| Mode | Part | Prints |
|---|---|---|
INDUSTRY_EXPOSURE | 1 | industry percentage for every industry |
ETF_LIQUIDATE | 2 | the minimum sold value, then the sold-share counts |
NBO_SPREAD | 3 | the percentage of passing quote rows |
TREE_VALIDATE | 4 | VALID or INVALID |
Part 1: Industry exposure
Holdings and prices arrive by ticker, but reports are organized by industry, so the values have to be joined and aggregated first. Mode INDUSTRY_EXPOSURE reports each industry's share of the portfolio's value.
- Each ticker belongs to exactly one listed industry.
- A ticker's portfolio value is
price * quantity. A ticker without a holding contributes zero. - Sum these values by industry and divide by the total portfolio value.
- The percentage is an integer number of tenths of a percent:
value * 1000 / total, rounded to the nearest integer with an exact half rounded upward. - If the total value is zero, every industry prints
0.0.
Input:
- the industry count
I, thenIlinesname K ticker1 ... tickerK; - the price count
P, thenPlinesticker price; - the holding count
H, thenHlinesticker quantity.
Output: one line per industry, in input order.
industry percentage
percentagehas one digit after the decimal point. For example, 887 tenths prints as88.7.
Limits and guarantees
- Every held ticker has a price and belongs to one industry.
- A ticker appears at most once among the prices and at most once among the holdings.
- Prices and quantities are non-negative integers.
- There are at most
10^5industries, tickers, prices, and holdings in total. - The total portfolio value is at most
10^15, so every intermediate value fits in a 64-bit integer.
Tests 1–5 and 17 cover this part.
Part 2: Liquidate ETF shares
The tool now needs to reduce concentrated stock exposure by selling whole shares of exchange-traded funds.
- Exchange-traded fund (ETF): a fund whose shares trade like a stock. Here, each ETF share holds a fixed positive quantity of each of its component stocks.
- Share value: the sum of
component quantity * stock priceover the ETF's components. - Dollar exposure of a stock: the value of that stock across every ETF share held, the sum over ETFs of
shares held * component quantity * stock price.
Mode ETF_LIQUIDATE asks for the cheapest sale:
- After selling some whole shares of each ETF, every stock's remaining dollar exposure must be at most
threshold. - Minimize the total value of the shares sold.
- If several sales reach the same minimum value, choose the lexicographically smallest vector of sold-share counts: compare the counts ETF by ETF in input order, and the first difference decides.
- A sale cannot exceed the shares held. A feasible sale is guaranteed.
Input:
- the ETF count
E, thenElinesname K stock1 qty1 ... stockK qtyK; - the holding count
H, thenHlinesetf_name shares; - the price count
P, thenPlinesstock price; - one line holding
threshold.
Output: two lines.
- Line 1: the minimum sold value.
- Line 2: the sold-share count of every ETF, in input order, separated by spaces.
Limits and guarantees
- An ETF missing from the holdings is held at zero shares.
- Every component stock has a price, and the stocks within one ETF are distinct.
- At most 6 ETFs, at most 8 components per ETF, and at most 8 held shares per ETF.
- Component quantities and prices are positive integers of at most
10^6, and0 <= threshold <= 10^15.
Tests 6–9 and 18 cover this part.
Part 3: Compare published quotes
Next, compare each internal quote with the published national best bid and offer.
- National best bid and offer (NBO): the highest bid and the lowest offer for the stock across all exchanges. It is the market-wide counterpart of one book's best bid and offer from Order Book API Shape. The offer is the ask.
- Basis point: one hundredth of a percent, so 10000 basis points is 100.00 percent.
A row passes only when both internal prices are within 10 percent of their NBO prices, inclusively:
abs(nbo_bid - internal_bid) * 10 <= nbo_bid
abs(nbo_offer - internal_offer) * 10 <= nbo_offer
- Timestamp fields are carried in each row but do not affect this version.
- Every row has equal weight.
- Mode
NBO_SPREADcomputes the percentage of passing rows as an integer number of basis points:passing * 10000 / rows, rounded to the nearest integer with an exact half rounded upward.
Input: the row count R, then R lines nbo_bid nbo_offer nbo_time internal_bid internal_offer internal_time.
Output: one line, the percentage with two digits after the decimal point. For example, 6667 basis points prints as 66.67.
Limits
- There are between 1 and
10^5rows. - Every price is a positive integer of at most
10^9.
Tests 10–12 and 19 cover this part.
Part 4: Validate a tree snapshot
Finally, the tool receives binary-tree snapshots as preorder text. Before accepting one, it must verify that the text describes the supplied tree exactly.
- The preorder serialization of a null child is
#. - For a node, it is the node's integer value, then the serialization of its left subtree, then that of its right subtree.
- Tokens are separated by whitespace.
- Mode
TREE_VALIDATEprintsVALIDonly when the candidate token sequence matches the entire serialization, andINVALIDotherwise. A wrong token, a missing token, or an extra token makes the candidate invalid.
Input:
- the node count
N, thenNlinesvalue left right; - one line holding the candidate tokens.
Nodes are indexed from 0 in input order, and node 0 is the root. left and right are child indices, or -1 for a null child.
Limits and guarantees
- The node list is a valid tree with
1 <= N <= 10^5. - Node values are 32-bit signed integers.
- A tree can be as deep as it has nodes.
Tests 13–16 cover this part.
12 INDUSTRY_EXPOSURE 2 Tech 3 Apple GOOG NVDA Automotive 2 Toyota GM 3 Apple 23 GOOG 17 GM 2 3 Apple 2 GOOG 1 GM 4
Tech 88.7 Automotive 11.3
The first line, 12, counts the lines after it, and lines[0] is the mode.
Apple is worth 23 × 2 = 46 and GOOG 17 × 1 = 17, so Tech holds 63; NVDA has no holding and adds nothing. GM is worth 2 × 4 = 8 and Toyota is not held, so Automotive holds 8.
The total is 71. Tech is 63 × 1000 / 71 ≈ 887.3 tenths, which rounds to 887 and prints as 88.7. Automotive is 8000 / 71 ≈ 112.7 tenths, which rounds to 113 and prints as 11.3.