Rs
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 apple

Output top 2 words by frequency (tie → alphabetical).

Steps

  1. Parse n and words
  1. HashMap frequency
  1. Collect (word, count); sort by count desc, word asc
  1. 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.