Dsa
Sorting
Easy
Book Title Sorting
You need to help a librarian sort a collection of book titles. Each title is composed of lowercase and uppercase letters, and should be sorted first by the number of words (titles with fewer words first) and, if there is a tie, alphabetically. Spaces separate the words in the title. Your task is to print the sorted list of titles.
Input Format:
- The first line contains an integer N (1 <= N <= 500), the number of titles.
- The next N lines each contain one book title.
Output Format:
- Print each title in the order determined by the sort criteria.
Example:
Input:
3
A Tale of Two Cities
The Great Gatsby
1984
Output:
1984
The Great Gatsby
A Tale of Two Cities
Key concepts
sortingstrings
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