If the number of trades is an ever-increasing cumulative value (which seems to be), you can simply support:
- Your hash file for updating the number of transactions in O (1).
- A simple array of 5 elements representing the 5 top tickers along with their volume.
When a new update arrives, you will update your hash file in O (1), and if the new number of transactions is large enough to enter the top fifth, you will also update your array. Checking the array and updating it can be done in O (1). You can also use binary search if you do not want to compare the new volume with all 5 values.
You cannot do much better than checking the average log (5).
If the number of transactions is not more monotonous (which seems unlikely, but it can happen if you want to consider the number of transactions made in a limited period of time), you can also support:
- Your hash (Ticker → Volume) for saving updates on ticker volumes.
- For the top five you need to use a bunch sorted by decreasing volumes.
When an update arrives, you will:
- Get the previous value (if any) of the volume for the ticker from the hash map in O (1)
- Remove the old value (if any) from the heap in O (log n) by looking at the old volume
- Add new value to heap in O (log n)
- Update new value in the hash map in O (1)
The overall complexity is O (log n), where "n" is the number of tickers.
Even if you need the first 5 tickers, when the volume of one ticker falls, you need to find the 6th element, which will become the 5th, and the 7th element .. and so on. I do not think that you can go much lower than O (log n) in complexity if the number of transactions is not monotonous.
source share