Hudson River Trading | Onsite | Design Scalable Hit Counter
I was asked essentially the same question as https://leetcode.com/problems/design-hit-counter/description/ but with a few twists.
A hit doesn't consist of a timestamp only in this version but also has a message (payload) attached to it. So besides the existing API from the original problem:
HitCounter()
void hit(int timestamp)
int getHits(int timestamp)we now also have something like:
vector<Payload_message> get_messages(int timestamp)for a given timestamp (in case we had multiple messages per timestamp).
Also, as long as the hits are relevant for the last 5 minutes as in the original problem, I was told that due to the potential large number of them (with each payload message taking up to a couple of KBs) it will not be possible to store the messages in the memory anymore (in contrast to the timestamps alone) and I should design a system to account for that.
So given the timestamps could all be stored in memory, I essentially started implementing a basic version of a SSTable (https://www.igvita.com/2012/02/06/sstable-and-log-structured-storage-leveldb/) with just one segment file to write there all the incoming messages. If I had more time, I would've also added some kind of multithreading to do the writing to disk & pruning older messages in a separate thread.
But I think I bombed the interview because I jumped straight to that SSTable implementation without answering more subtle interviewer's questions - which they hinted at only towards the end. For example:
- How to decide whether every message should be cached to disk immediately rather than additionally maintaining some sort of in-memory only cache as well (I assume it was supposed to be an LRU kind of cache)? <-- how could I determine that based on the load? how could I roughly measure the load in the first place, possibly with some imaginary figures?
- Would there be any (possibly better) other solutions here besides using SSTable for disk caching? <-- would there?


