Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Checkpoint: STL in Combination

โญโญ intermediateโณ 35 min read๐Ÿ“ Lesson 96 of 204

Top-K frequent words: counting, ordering, and a tie-break that punishes hand-wavy solutions.

top_k is the module's exam: it needs a counting map, an ordering, and a tie-break โ€” all three from this module's lessons.

  • counting: std::map<std::string, int> with operator[]'s insert-then-increment
  • ordering: count descending
  • tie-break: lexicographic (smaller word first) โ€” decide this BEFORE coding

Sorting a vector of (count, word) pairs with a comparator that checks count != count first is the cleanest deterministic form. A priority_queue with the same comparator works equally well.

If your tie-break test fails while the basic test passes, your comparator is silently depending on map iteration order or input order โ€” exactly the nondeterminism this course is training you to eliminate.