What would be a clean design for an app that is to store transactions and return a overview of the ones received in the last 60 seconds, operating in constant time and memory (O(1))?
The app should expose two endpoints, one of them to
POST a new transaction, and the other one to
GET the statistics based of the transactions of the last 60 seconds. As said, both need to run in O(1).
A few weeks ago I was given this requirement as part of a code challenge, so let's assume a small app in a single machine. My approach was saving the transactions to both DB and a cache, and having a periodic task, triggered every 1ms, removing entries older than 60s from the cache. Then, fetching the statistics from it was simple. I think this results in O(1) for both endpoints, but there is obviously some serious problem with this approach because I got rejected straight away.