Dsa
Sorting
Easy
Sort Book Titles by Length
You are tasked with sorting a list of book titles based on their lengths. If two titles have the same length, they should be sorted alphabetically.
Input format:
- The first line contains an integer n, the number of book titles.
- The next n lines each contain one book title.
Output format:
- Print the titles sorted by length and lexicographically where needed, each on a new line.
Example input:
4
The Great Gatsby
War and Peace
1984
To Kill a Mockingbird
Output:
1984
War and Peace
The Great Gatsby
To Kill a Mockingbird
Key concepts
sortingstringslist
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