Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to perform a linear search that returns the index of the target or -1 if not found.
DSA C
int linear_search(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == [1]) {
return i;
}
}
return -1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[i] with size or i instead of target.
Returning the wrong index or value.
✗ Incorrect
The code compares each element arr[i] with the target value to find a match.
2fill in blank
mediumComplete the code to calculate the middle index for binary search.
DSA C
int mid = [1] + (high - low) / 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using high alone or zero instead of low in the calculation.
Calculating mid incorrectly causing infinite loops.
✗ Incorrect
The middle index is calculated by adding low to half the distance between high and low.
3fill in blank
hardFix the error in the binary search condition to check if the target is less than the middle element.
DSA C
if (target [1] arr[mid]) { high = mid - 1; } else if (target > arr[mid]) { low = mid + 1; } else { return mid; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causing wrong search direction.
Using equality or inequality operators incorrectly.
✗ Incorrect
To move the search to the left half, check if target is less than arr[mid].
4fill in blank
hardFill both blanks to create a loop that runs while low is less than or equal to high.
DSA C
while ([1] <= [2]) { int mid = low + (high - low) / 2; // binary search steps }
Attempts:
3 left
💡 Hint
Common Mistakes
Using size or target instead of high in the condition.
Using incorrect comparison operators.
✗ Incorrect
The loop continues while low index is less than or equal to high index.
5fill in blank
hardFill all three blanks to create a dictionary comprehension-like structure in C style pseudocode for counting elements greater than zero.
DSA C
for (int [1] = 0; [1] < size; [1]++) { if (arr[[3]] [2] 0) { count++; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for loop and indexing.
Using '<' instead of '>' causing wrong condition.
✗ Incorrect
Use 'i' as loop variable, check if arr[i] > 0, and use 'i' again for indexing.
