Dsa
Sorting
Easy
Recipe Ingredient Sorter
You are tasked with sorting recipe ingredients based on their quantity and then by their names. The input consists of an integer N followed by N lines where each ingredient has a name and a quantity separated by a space. The output should present the list of ingredients sorted by quantity in ascending order. If two ingredients have the same quantity, sort them alphabetically by their names.
Input Format:
N
ingredient1 quantity1
ingredient2 quantity2
...
ingredientN quantityN
Output Format:
ingredient1 quantity1
ingredient2 quantity2
...
ingredientN quantityN
Example:
Input:
5
Sugar 200
Flour 500
Eggs 200
Butter 100
Salt 50
Output:
Salt 50
Butter 100
Sugar 200
Eggs 200
Flour 500
Key concepts
sortingstringsdictionaries
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