Dsa
Binary Search
Easy
Library Book Search
A librarian maintains a sorted list of book IDs. Your task is to determine the position of a given book ID in the list. If the book ID exists, return its index; if it's not found, return -1.
Input format:
- The first line contains an integer n (1 <= n <= 10^5), the number of books.
- The second line contains n space-separated integers representing book IDs.
- The third line contains an integer book_id representing the ID you want to search for.
Output format:
- Print the index of the book ID if it exists, or -1 if it does not.
Example:
Input:
4
101 103 104 105
103
Output:
1
Key concepts
binary_searchsearcharray
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