Dsa
Sorting
Medium
Custom Sort Management System
You are tasked with managing a sorting system for an inventory of products. Each product has a name and a category, and you need to sort the list of products based on a given custom order of categories. If two products belong to the same category, they should be sorted alphabetically by name. Your program should read products and a specified category order, and return the sorted list of product names.
Input:
The first line contains an integer N (1 <= N <= 1000), the number of products.
The next N lines contain a product name (a string) followed by its category (a string).
The last line contains a string of space-separated categories representing the order to sort.
Output:
Print the sorted list of product names, one per line.
Example:
Input:
4
Apple Fruits
Carrot Vegetables
Banana Fruits
Zucchini Vegetables
Fruits Vegetables
Output:
Apple
Banana
Carrot
Zucchini
Key concepts
sortingcustom_orderstability
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