Top K Frequent Words
Signals to notice
Brute force first
Count frequencies, sort all words by frequency then alphabetically. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.
The key insight
Count frequencies with hash map, use a min-heap of size k with custom comparator (lower frequency first, higher alphabetical first for ties). Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.
What must stay true
The min-heap evicts the 'worst' candidate — lowest frequency, or alphabetically later for ties. After processing all words, the heap contains the top k in reverse order. As long as that statement keeps holding, you can trust the steps built on top of it.
Easy way to go wrong
Wrong tie-breaking in the heap comparator — for equal frequency, the lexicographically LARGER word should be at the top of the min-heap (to be evicted first), keeping the smaller word. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.