Question bank › Heap
Dsa Heap Hard

K Most Frequent Words

You are given a list of words. Your task is to find the K most frequent words. If two words have the same frequency, return them in alphabetical order. Input consists of two lines, the first containing the space-separated words, and the second containing the integer K. Output the K most frequent words, each on a new line. Example: Input: apple banana apple orange banana grape 2 Output: banana apple

Key concepts

heapfrequencysorting

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.