Question bank › Heap
Dsa Heap Hard

Top Frequent Words

You are given a string of words separated by spaces. Write a function to find the top K most frequent words in the string. In case of a tie, return them in alphabetical order. Input Format: - The first line contains two integers N (the total number of words) and K (the number of top words to return). - The second line contains a string containing N words. Output Format: - Print the top K words separated by spaces. Example Input: 5 2 apple banana apple orange banana mango Example Output: banana apple

Key concepts

heapfrequency_countsorting

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
Part of InterviewLab's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.