📖 Scenario: Imagine you have a list of book titles sorted alphabetically on a shelf. You want to find if a certain book is there quickly without checking every book one by one.
🎯 Goal: You will create a sorted list of book titles, set a target book to find, use binary search to check if the book is in the list, and print the result.
📋 What You'll Learn
Create a sorted list of book titles with exact values
Create a string variable for the target book title
Use a binary search method to find the target in the list
Print whether the target book was found or not
💡 Why This Matters
🌍 Real World
Searching sorted data quickly is common in apps like phone contacts, dictionaries, or product catalogs.
💼 Career
Understanding binary search and sorted data is fundamental for software developers to write efficient search features.
Progress0 / 4 steps
1
Create a sorted list of book titles
Create a List<string> called books with these exact titles in this order: "Algorithms", "Data Structures", "Design Patterns", "Networking", "Operating Systems".
DSA C#
Hint
Use List<string> and initialize it with the exact book titles in sorted order.
2
Set the target book title to search
Create a string variable called targetBook and set it to "Design Patterns".
DSA C#
Hint
Use a string variable named targetBook and assign the exact value.
3
Use binary search to find the target book
Use books.BinarySearch(targetBook) and store the result in an int variable called index.
DSA C#
Hint
Use the BinarySearch method on books with targetBook as argument and save the result in index.
4
Print if the target book was found
Print "Found" if index is greater than or equal to 0, otherwise print "Not Found".
DSA C#
Hint
Check if index is >= 0 to decide if the book was found, then print the correct message.
Practice
(1/5)
1. Why is it important for a list to be sorted before applying binary search?
easy
A. Because binary search can only find the first element in any list
B. Because binary search works only on lists with unique elements
C. Because binary search divides the list and compares values assuming order
D. Because binary search requires the list to be reversed
Solution
Step 1: Understand binary search working principle
Binary search splits the list into halves and compares the middle element to the target, assuming the list is sorted.
Step 2: Importance of sorted order
If the list is not sorted, the halves cannot be correctly chosen, so binary search fails to find the target efficiently.
Final Answer:
Because binary search divides the list and compares values assuming order -> Option C
Quick Check:
Sorted list needed for binary search = True [OK]
Hint: Binary search needs order to split search space correctly [OK]
Common Mistakes:
Thinking binary search works on unsorted lists
Assuming binary search finds duplicates only
Confusing binary search with linear search
2. Which of the following is the correct syntax to start a binary search on a sorted array named arr in C#?
easy
A. int index = Array.BinarySearch(arr, target);
B. int index = arr.BinarySearch(target);
C. int index = BinarySearch(arr, target);
D. int index = arr.SearchBinary(target);
Solution
Step 1: Recall C# binary search method usage
C# provides a static method Array.BinarySearch that takes the array and the target value.
Step 2: Check method call correctness
int index = Array.BinarySearch(arr, target); correctly calls Array.BinarySearch(arr, target). Other options use invalid syntax or methods.
Final Answer:
int index = Array.BinarySearch(arr, target); -> Option A
Quick Check:
Correct C# binary search syntax = int index = Array.BinarySearch(arr, target); [OK]
Hint: Use Array.BinarySearch for arrays in C# [OK]
Common Mistakes:
Trying to call BinarySearch as instance method on array
Using wrong method names like SearchBinary
Missing the Array class prefix
3. What will be the output of the following C# code?
int[] arr = {2, 4, 6, 8, 10};
int index = Array.BinarySearch(arr, 6);
Console.WriteLine(index);
medium
A. -1
B. 2
C. 3
D. 0
Solution
Step 1: Understand array and target
The array is sorted: [2, 4, 6, 8, 10]. The target is 6.
Step 2: Find index of target in array
6 is at index 2 (0-based indexing).
Final Answer:
2 -> Option B
Quick Check:
Index of 6 in array = 2 [OK]
Hint: BinarySearch returns index of found element [OK]
Common Mistakes:
Confusing index with value
Assuming 1-based indexing
Expecting -1 when element is present
4. The following code tries to find the index of 5 in a sorted array but gives an unexpected result. What is the error?
int[] arr = {1, 3, 7, 9, 11};
int index = Array.BinarySearch(arr, 5);
Console.WriteLine(index);
medium
A. 5 is not in the array, so negative index is returned
B. The method call syntax is incorrect
C. BinarySearch cannot be used on integers
D. The array is not sorted
Solution
Step 1: Check if array is sorted
The array [1, 3, 7, 9, 11] is sorted in ascending order.
Step 2: Understand BinarySearch return when element not found
When the target (5) is not found, BinarySearch returns a negative number indicating the bitwise complement of the insertion point.
Final Answer:
5 is not in the array, so negative index is returned -> Option A
Quick Check:
Not found element returns negative index [OK]
Hint: Negative result means element not found [OK]
Common Mistakes:
Thinking array is unsorted
Expecting -1 always when not found
Believing syntax is wrong
5. You have a sorted list of student scores: int[] scores = {55, 67, 78, 89, 92, 95}. You want to find if a score of 80 exists using binary search. If not found, you want to insert 80 in the correct position to keep the list sorted. Which approach correctly achieves this?
hard
A. Sort the array after inserting 80 at the end
B. Use Array.BinarySearch and insert at returned index directly
C. Use linear search to find 80 and insert if missing
D. Use Array.BinarySearch to find 80; if negative, insert at ~index returned
Solution
Step 1: Use Array.BinarySearch to find 80
BinarySearch returns index if found; if not found, returns negative number which is bitwise complement of insertion point.
Step 2: Insert 80 at correct position
If result is negative, invert bits (~index) to get insertion position to keep array sorted.
Final Answer:
Use Array.BinarySearch to find 80; if negative, insert at ~index returned -> Option D
Quick Check:
Negative index means insert at bitwise complement [OK]
Hint: Insert at ~index if BinarySearch returns negative [OK]
Common Mistakes:
Inserting at negative index directly
Not using binary search result properly
Sorting after insertion instead of correct position
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.