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 this C code that performs a linear search for the value 7 in the array?
DSA C
int arr[] = {3, 5, 7, 9, 11}; int n = 5; int target = 7; int i; int index = -1; for(i = 0; i < n; i++) { if(arr[i] == target) { index = i; break; } } printf("%d", index);
Attempts:
2 left
💡 Hint
Remember that array indexing in C starts at 0.
✗ Incorrect
The target value 7 is at index 2 in the array. The loop stops when it finds 7 and prints 2.
🧠 Conceptual
intermediateLinear Search Worst Case Scenario
In linear search, what is the worst case time complexity when searching for an element in an array of size n?
Attempts:
2 left
💡 Hint
Think about how many elements you might check if the target is not in the array.
✗ Incorrect
In the worst case, linear search checks every element once, so the time complexity is O(n).
🔧 Debug
advancedFind the Bug in Linear Search Implementation
What error will this C code produce when searching for 4 in the array?
DSA C
int arr[] = {1, 2, 3, 5, 6}; int n = 5; int target = 4; int i; int index = -1; for(i = 0; i < n; i++) { if(arr[i] == target) { index = i; break; } } printf("%d", index);
Attempts:
2 left
💡 Hint
Check the loop condition carefully for array bounds.
✗ Incorrect
The original loop condition was incorrect (i <= n). It should be i < n to avoid out of bounds access. With the corrected loop, the code runs and prints -1 since 4 is not in the array.
🚀 Application
advancedLinear Search on Linked List
Given a singly linked list, which approach correctly implements linear search to find a target value?
Attempts:
2 left
💡 Hint
Think about how linked lists are structured and accessed.
✗ Incorrect
Linear search on linked list means checking each node one by one from the start until the target is found or list ends.
❓ Predict Output
expertOutput of Linear Search with Multiple Occurrences
What is the output of this C code searching for 3 in the array with multiple 3s?
DSA C
int arr[] = {1, 3, 5, 3, 7}; int n = 5; int target = 3; int i; int index = -1; for(i = 0; i < n; i++) { if(arr[i] == target) { index = i; break; } } printf("%d", index);
Attempts:
2 left
💡 Hint
Linear search stops at the first match.
✗ Incorrect
The first occurrence of 3 is at index 1, so the loop breaks and prints 1.
