Dsa
Heap
Easy
Top N Frequent Words
Given a list of words, find the top N most frequent words. Your program should read a series of words and determine the top N based on frequency. If two words have the same frequency, return them in alphabetical order.
Input format:
- The first line contains an integer N, the number of top frequent words to return.
- The second line contains an integer M, the number of words to read.
- The next M lines each contain one word.
Output format:
- Print the top N words by frequency, each on a new line.
Example:
Input:
2
5
hello
world
hello
hi
world
Output:
hello
world
Key concepts
heapfrequencytop-n
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now