Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the first occurrence of the target in the sorted array.
DSA C
int firstOccurrence(int arr[], int n, int target) {
int low = 0, high = n - 1, result = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
result = mid;
high = mid [1] 1;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes infinite loop or wrong index.
Not updating high pointer after finding target.
✗ Incorrect
When the target is found, we move the high pointer to mid - 1 to continue searching the left half for the first occurrence.
2fill in blank
mediumComplete the code to find the last occurrence of the target in the sorted array.
DSA C
int lastOccurrence(int arr[], int n, int target) {
int low = 0, high = n - 1, result = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
result = mid;
low = mid [1] 1;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes infinite loop or wrong index.
Not updating low pointer after finding target.
✗ Incorrect
When the target is found, move the low pointer to mid + 1 to continue searching the right half for the last occurrence.
3fill in blank
hardFix the error in the countOccurrences function to correctly calculate the count of target in the array.
DSA C
int countOccurrences(int arr[], int n, int target) {
int first = firstOccurrence(arr, n, target);
if (first == -1) return 0;
int last = lastOccurrence(arr, n, target);
return last [1] first + 1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes wrong count.
Forgetting to add 1 after subtraction.
✗ Incorrect
The count is last index minus first index plus one, so we subtract first from last and add 1.
4fill in blank
hardFill both blanks to complete the binary search condition and update pointers correctly.
DSA C
while (low [1] high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { result = mid; high = mid [2] 1; } else if (arr[mid] < target) { low = mid + 1; } else { high = mid - 1; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '<=' in loop condition causes missing last element.
Using '+' instead of '-' when updating high pointer.
✗ Incorrect
The loop continues while low is less than or equal to high, and when target is found, high moves to mid - 1 to search left.
5fill in blank
hardFill both blanks to create a dictionary-like structure in C that maps each unique element to its count.
DSA C
typedef struct {
int key;
int value;
} Pair;
void countElements(int arr[], int n, Pair result[], int *size) {
*size = 0;
for (int i = 0; i < n; i++) {
int found = 0;
for (int j = 0; j < *size; j++) {
if (result[j].key == arr[i]) {
result[j].value [1] 1;
found = 1;
break;
}
}
if (!found) {
result[*size].key = arr[i];
result[*size].value = 1;
(*size)[2];
}
}
}
int main() {
int arr[] = {1, 2, 2, 3, 3, 3};
int n = sizeof(arr) / sizeof(arr[0]);
Pair result[10];
int size = 0;
countElements(arr, n, result, &size);
for (int i = 0; i < size; i++) {
printf("%d -> %d\n", result[i].key, result[i].value);
}
return 0;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '--' or '-=' decreases counts or size incorrectly.
Using '+' alone without '=' does not update the variable.
✗ Incorrect
To increment the count, use '+='; to increase size, use '++'. The first blank increments value by 1, the second increments size by 1.
