Suppose you have a stream of stock events with a requirement to retrieve the top K aggregated summations by stock symbol in descending order for only the last 20 seconds. Each event is a tuple with a time, stock symbol, and value. Timestamps are guaranteed to be in increasing order, but not unique. You have can have multiple events come with the same timestamp. What's the most optimal way of doing this? The brute force approach would be to recompute the aggregates each time the retrieve function is called, so you can account for events that dropped off the 20 second window.
For example
Apple 1s 10
Apple 2s 10
Tesla 5s 10
Google 10s 50
Apple 19s 15
--------- retrieve top 2 called ---- (Google, 50), (Apple, 35)
Tesla 25s 20
----- retrieve top 2 called -------(Google, 50), (Tesla, 30)