Dsa
Sorting
Medium
Inventory Item Sorting
Your company has an inventory system that lists various items, each with a name and quantity. You need to implement a program that reads a list of items and sorts them by their quantities in ascending order. If two items have the same quantity, sort them alphabetically by their names. Input will consist of an integer N (the number of items), followed by N lines each containing the name of the item and its quantity separated by a space. Output the sorted list in the format 'Name - Quantity'.
Example Input:
5
Pencils 20
Notebooks 15
Erasers 20
Markers 10
Rulers 10
Example Output:
Markers - 10
Rulers - 10
Notebooks - 15
Pencils - 20
Erasers - 20
Key concepts
sortingarrayslambda
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