Top-K Frequent Words With Lexicographic Tie-Break
Difficulty: Easy · Unit 4: Files, Exceptions & Standard Library · Featured in: CodeTantra Lab Pattern
Given an integer K and a list of N lowercase words, print the K most frequent words ordered from highest frequency to lowest. Words with equal frequency must be ordered alphabetically (ascending).
Input & Output Format
Input: Line 1: two integers N and K. Line 2: N space-separated words.
Output: Print the top K words separated by single spaces.
Sample 1
6 2 i love coding i love python
i love
'i' (2) and 'love' (2) tie and are sorted alphabetically.
Free forever — sign in with email at poorvithmp.com to unlock the dry run, verified Python 3 solution, edge-case traps, and downloads.