Question bank › Sorting
Dsa Sorting Hard

Reorder Books

You are given a list of books, each identified by a title and an author's name. Your task is to sort the books first by the author's name in ascending order, and then by the title in ascending order if the authors are the same. Output the sorted list of books in the format 'Author: Title'. Input: The first line contains an integer n (1 ≤ n ≤ 1000), the number of books. The next n lines each contain a string representing the book's title and the author's name in the format 'Title by Author'. Output: Print the sorted list of books in the specified format. Example: Input: 4 The Great Gatsby by F. Scott Fitzgerald To Kill a Mockingbird by Harper Lee 1984 by George Orwell Pride and Prejudice by Jane Austen Output: Pride and Prejudice by Jane Austen To Kill a Mockingbird by Harper Lee 1984 by George Orwell The Great Gatsby by F. Scott Fitzgerald

Key concepts

sortingarrayscustom_sort

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.