Skip to main content

Checkpoint: Top-K frequent words

Challenge on lesson: Checkpoint: STL in Combination

Implement std::vector<std::string> top_k(const std::vector<std::string>& words, std::size_t k) returning the k most frequent words ordered by count descending; ties are broken lexicographically (smaller word first). If there are fewer than k distinct words, return all of them (still ordered). Combine a counting std::map with a sort (or a std::priority_queue) — the tie-break rule is the part most solutions get wrong.

Difficulty: intermediate

Back to lesson: Checkpoint: STL in Combination