Remix.run Logo
▲ alexfoo 2 hours ago

Indeed. That's what I mean by "some people just want to get the stars and move on".

Taking a deeper look can provide some very useful insights and a better understanding of the more general problem which often translates into real world scenarios in companies that operate at scale.

Optimising something that sorts 20 values is often pointless unless it really lies on a hot path and is called very frequently. It's more about when something that was written to sort ~100 values suddenly starts to become a bottleneck as the service grows and grows unexpectedly and is suddenly needing to sort 100k values, then 100M values, etc.

Taking this particular problem as an example, I've had candidates who run the complete spectrum from claiming it's impossible to find the top 3 and bottom 3 values without sorting the entire list of frequency counts (one even claimed we'd have to sort the raw input list for any chance of getting an answer), to the candidate that says "...once you have the frequency counts then finding the final answer is just a pair of max-heaps of size 3, but this depends on whether you can hold the set of frequency counts in memory or not, if not we'd have to take a different approach. Also, max-heap might seem overkill, if you wanted highest and lowest you wouldn't use a pair of max-heaps of size 1, but 3 is about the cutoff where manual tracking of the top/bottom values becomes tricky, and using a heap makes it trivial to move to 4, 5 or any other value if that becomes a need in the future".

(To be clear, I've never asked/discussed this specific question in an interview, I'm just translating responses for a similar problem.)