Debugging Contest Question Set
Debugging Contest Question Set
Problem: You are given the following code that attempts to calculate the sum of the
elements in an array, but there’s an indexing bug.
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int sum = 0;
Problem: The following code snippet aims to copy an array of integers into another array.
However, due to improper handling of the array size, it causes a memory overwrite issue.
#include <stdio.h>
#include <string.h>
int main() {
int source[] = {10, 20, 30, 40, 50};
int dest[4];
copy_array(source, dest, 5);
return 0;
}
Task: Find and fix the issue that causes an out-of-bounds write and potential memory
corruption.
Problem: The following program attempts to reverse an array using pointer arithmetic.
However, due to a logical error, the reversal does not occur correctly.
#include <stdio.h>
start++;
end--;
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
reverse_array(arr, n);
return 0;
}
Task: Find the logical error in the array reversal function and correct it.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int size = 5;
for (int i = 1; i < size; i++) { // Bug: starts from index 1 instead
of 0
arr[i] = i * 10;
}
free(arr);
return 0;
}
Task: Identify the mistake in array indexing and suggest the correct approach to fix the code.
Problem: The following code is supposed to find the maximum and minimum values in an
array. However, it contains an error that leads to undefined behavior due to invalid indexing.
#include <stdio.h>
int main() {
int arr[] = {4, 9, 2, 7, 5};
int n = sizeof(arr) / sizeof(arr[0]);
find_max_min(arr, n);
return 0;
}
Task: Find the issue with array indexing in the find_max_min function and fix it.