Dsa
Sorting
Easy
Sort Words by Frequency
Given a sentence, sort the words based on their frequency of occurrence in descending order. If two words have the same frequency, sort them alphabetically.
Input format:
- A single line containing the sentence (words are separated by spaces).
Output format:
- Print the words sorted by their frequency, each on a new line.
Example input:
hello world hello my name is my
Output:
hello
my
is
name
world
Key concepts
sortingdictionariescollections
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