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

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_toolkit as lines.
  • 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.
ModePartPrints
INDUSTRY_EXPOSURE1industry percentage for every industry
ETF_LIQUIDATE2the minimum sold value, then the sold-share counts
NBO_SPREAD3the percentage of passing quote rows
TREE_VALIDATE4VALID 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.

  1. Each ticker belongs to exactly one listed industry.
  2. A ticker's portfolio value is price * quantity. A ticker without a holding contributes zero.
  3. Sum these values by industry and divide by the total portfolio value.
  4. 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.
  5. If the total value is zero, every industry prints 0.0.

Input:

  • the industry count I, then I lines name K ticker1 ... tickerK;
  • the price count P, then P lines ticker price;
  • the holding count H, then H lines ticker quantity.

Output: one line per industry, in input order.

industry percentage
  • percentage has one digit after the decimal point. For example, 887 tenths prints as 88.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^5 industries, 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 price over 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:

  1. After selling some whole shares of each ETF, every stock's remaining dollar exposure must be at most threshold.
  2. Minimize the total value of the shares sold.
  3. 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.
  4. A sale cannot exceed the shares held. A feasible sale is guaranteed.

Input:

  • the ETF count E, then E lines name K stock1 qty1 ... stockK qtyK;
  • the holding count H, then H lines etf_name shares;
  • the price count P, then P lines stock 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, and 0 <= 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
  1. Timestamp fields are carried in each row but do not affect this version.
  2. Every row has equal weight.
  3. Mode NBO_SPREAD computes 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^5 rows.
  • 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.

  1. The preorder serialization of a null child is #.
  2. For a node, it is the node's integer value, then the serialization of its left subtree, then that of its right subtree.
  3. Tokens are separated by whitespace.
  4. Mode TREE_VALIDATE prints VALID only when the candidate token sequence matches the entire serialization, and INVALID otherwise. A wrong token, a missing token, or an extra token makes the candidate invalid.

Input:

  • the node count N, then N lines value 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.

Example
Input
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
Output
Tech 88.7
Automotive 11.3
Explanation

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.