Dsa
Binary Search
Hard
Shopping List Item Search
You are tasked with implementing a shopping list feature for a mobile application. Given a sorted shopping list and a target item, check if the item is on the list and return its index. If the item is not found, return -1. Your program should read the sorted list of items and a target item, and print the index of the target item or -1 if it is not present.
Input Format:
- The first line contains an integer n (1 <= n <= 100,000) representing the number of items on the list.
- The next n lines contain the items (1 <= length of item <= 50).
- The last line contains the target item.
Output Format:
- Print the index of the target item, or -1 if not found.
Example:
Input:
4
Apples
Bananas
Cherries
Dates
Cherries
Output:
2
Key concepts
binary_searchsortinglists
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