Problem · Database

Market Share Ticker with Imbalance

MediumWolverine Trading logoWolverine TradingNEW GRADINTERNOA

Problem statement

An exchange publishes market trades, and one participant separately records the subset of trades in which it participated. A symbology table maps exchange product identifiers to ticker symbols.

For every product in which the participant traded, compute its market share as the participant's total traded quantity divided by the exchange's total traded quantity for that product. Select the product with the largest exact market-share ratio.

For the selected product, compute the participant's closing imbalance: total quantity on buy rows (B) minus total quantity on sell rows (S).

Return exactly one row with columns product and imbalance, in that order. FastPrep represents the source routine's two-key dictionary as this one-row result table.

Table schema

MySQLPostgreSQLPandas

Use the same input data with any supported language. Open the Schema tab in the editor to see the generated SQL setup or Pandas DataFrames.

market_trades

One row per market transaction.

ColumnTypeNullableDescription
product_idIntegerNoExchange product identifier.
trade_idPKIntegerNoUnique exchange transaction identifier.
quantityIntegerNoTotal exchange shares in the transaction.

participant_trades

The participant's subset of exchange transactions.

ColumnTypeNullableDescription
productTextNoTicker symbol.
trade_idPKIntegerNoReferenced exchange transaction identifier.
sideTextNoB for buy or S for sell.
quantityIntegerNoShares executed by the participant.

Foreign key: trade_id market_trades(trade_id)

symbology

Mapping from ticker symbols to exchange product identifiers.

ColumnTypeNullableDescription
productTextNoTicker symbol.
product_idPKIntegerNoExchange product identifier.

Expected result

Your query or function must return these columns.

ColumnTypeNullableDescription
productTextNo
imbalanceIntegerNo

Row order: must match exactly. Numeric tolerance: 0.

Constraints

  • market_trades.trade_id, symbology.product, and symbology.product_id are unique.
  • Every participant row references one market trade, and its product matches that trade's mapped symbology product. Products with no participant row are not candidates.
  • Every participant product has a positive total exchange quantity. Market and participant quantities are positive integers, and all required sums fit a signed 64-bit integer.
  • side is exactly B or S, and at least one participant row is present.
  • Compare market-share ratios without rounding. If products tie, return the lexicographically smaller uppercase ASCII product.

More Wolverine Trading problems