Skip to content

Tracker for top tickers by cumulative volume

Question

Design a class that ingests a stream of trades and supports two operations: execute_trade(ticker, volume) records a trade, and most_traded(k) returns the kk tickers with the largest total volume as strings of the form TICKER VOLUME. For example, after trades TSLA 1000, NFLX 700, TSLA 200 and META 1400, asking for the top 22 should give META 1400 then TSLA 1200. Explain your design and complexities.

Short answer

A hash map of totals plus a size-kk min-heap at query time. execute_trade adds the volume to the ticker's running total in O(1)O(1). most_traded(k) pushes each of the nn tickers into a min-heap capped at kk (popping the smallest when it overflows), then sorts the survivors descending: O(nlog⁡k)O(n\log k) time, O(n)O(n) storage. If queries dominate, keep a balanced tree keyed by volume instead.

Adapted from OpenQuant; question and answer rewritten by SelfMadeQuant.com.

See the hint and the full worked solution in the question bank (free account).

Open the worked solution

Lessons behind this question

Start with a free lesson: Probability Fundamentals is part of a free topic. Create a free account, no card needed, to read it in full with worked solutions.

Read it free