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 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 should give META 1400 then TSLA 1200. Explain your design and complexities.
Short answer
A hash map of totals plus a size- min-heap at query time. execute_trade adds the volume to the ticker's running total in . most_traded(k) pushes each of the tickers into a min-heap capped at (popping the smallest when it overflows), then sorts the survivors descending: time, 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 solutionLessons behind this question
- Algorithms, Growth Rates, and Asymptotic Notation
- Quicksort and Randomized Quicksort
- Sample Spaces, Events, and the Probability Axioms
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