Module 4Interview Patterns LabLesson 23 of 2325 min
Lab: End-to-End Mini Problem
Parse input, count with map, sort top frequencies, use heap optional.
Problem
Input (simulated):
8
apple banana apple orange banana apple pear appleOutput top 2 words by frequency (tie → alphabetical).
Steps
- Parse n and words
- HashMap frequency
- Collect (word, count); sort by count desc, word asc
- Print top k
Challenge
Solve with BinaryHeap instead of full sort for general large k.
Tips
- Parse + map + sort
- Tie-break alphabetical
- Heap for large n top-k
Try it live
Lab: End-to-End Mini ProblemRust 2021
Loading editor…
Output
Press Run to compile and execute.