Challenge - 5 Problems
Linear Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Linear Search on Array
What is the output of the following C# code that performs a linear search for the value 7 in the array?
DSA C#
int[] arr = {3, 5, 7, 9, 11}; int target = 7; int index = -1; for (int i = 0; i < arr.Length; i++) { if (arr[i] == target) { index = i; break; } } Console.WriteLine(index);
Attempts:
2 left
💡 Hint
Remember that array indexing in C# starts at 0.
✗ Incorrect
The value 7 is at index 2 in the array. The loop stops when it finds 7 and prints the index 2.
🧠 Conceptual
intermediateNumber of Comparisons in Worst Case
If you perform a linear search for a value that is not present in an array of length 10, how many comparisons will the algorithm make?
Attempts:
2 left
💡 Hint
Think about how many elements the algorithm checks before concluding the value is not found.
✗ Incorrect
In the worst case, linear search checks every element once. For 10 elements, it makes 10 comparisons.
🔧 Debug
advancedIdentify the Bug in Linear Search Implementation
What error will the following C# code produce when searching for the value 4 in the array?
DSA C#
int[] arr = {1, 2, 3, 5, 6}; int target = 4; int index = 0; for (int i = 0; i <= arr.Length; i++) { if (arr[i] == target) { index = i; break; } } Console.WriteLine(index);
Attempts:
2 left
💡 Hint
Check the loop condition carefully and remember array indexing rules.
✗ Incorrect
The loop uses i <= arr.Length which tries to access arr[arr.Length], causing IndexOutOfRangeException.
🚀 Application
advancedLinear Search on Linked List
Given a singly linked list with nodes containing values [10, 20, 30, 40], what is the output index when searching for value 30 using linear search?
DSA C#
class Node { public int val; public Node next; public Node(int v) { val = v; next = null; } } Node head = new Node(10); head.next = new Node(20); head.next.next = new Node(30); head.next.next.next = new Node(40); int target = 30; int index = 0; Node current = head; while (current != null) { if (current.val == target) break; current = current.next; index++; } Console.WriteLine(current == null ? -1 : index);
Attempts:
2 left
💡 Hint
Count nodes starting from 0 until you find the target value.
✗ Incorrect
The value 30 is in the third node, which is index 2 starting from 0.
🧠 Conceptual
expertTime Complexity of Linear Search in Different Cases
What is the average time complexity of linear search in an unsorted array of size n?
Attempts:
2 left
💡 Hint
Think about how many elements are checked on average before finding the target.
✗ Incorrect
On average, linear search checks half the elements, which is proportional to n, so time complexity is O(n).
