Question bank › Sorting
Dsa Sorting Hard

Custom Sorting for Inventory

You manage an inventory of items, each identified by a name and a quantity. Your goal is to sort the items primarily by quantity (in descending order) and then by name (in alphabetical order if quantities are equal). Please format the output as 'Name: Quantity'. Input: The first line contains an integer n (1 ≤ n ≤ 1000), the number of items. The next n lines each contain a string followed by an integer representing the item name and the quantity, separated by a space. Output: Print the sorted list of items. Example: Input: 4 Apple 20 Banana 10 Cherry 20 Date 5 Output: Apple: 20 Cherry: 20 Banana: 10 Date: 5

Key concepts

sortingstringscustom_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.