testing-debugging · beginner · ~15 min
Debug and prevent reverse-loop off-by-one errors and unsigned index underflows.
Off-by-one errors in reverse loops frequently lead to out-of-bounds array reads and skipped initial elements. When iterating backwards with size_t, decrementing past zero wraps around to SIZE_MAX.
Implement:
int find_last_occurrence(const int *arr, size_t len, int target);
Find the zero-based index of the last occurrence of target in array arr of size len.
arr == NULL or len == 0, return -1.int.target does not appear in arr, return -1.int a[] = {10, 20, 30, 20, 40};
find_last_occurrence(a, 5, 20); // returns 3 (not 1)
arr: pointer to integer array; len: size_t element count; target: integer search value.
Returns zero-based index of last match, or -1 if not found or invalid.
Zero dynamic allocations. Safe loop termination with unsigned size_t.
#include <stddef.h>
/* Find zero-based index of last occurrence of target in arr. Return -1 if not found or arr is NULL. */
int find_last_occurrence(const int *arr, size_t len, int target) {
(void)arr; (void)len; (void)target;
return -1;
}
Writing i >= 0 with size_t resulting in an infinite loop; indexing arr[i] when i starts at len (out of bounds).
len == 0 returns -1; single-element match returns 0; target at index 0 found correctly; duplicate elements return last index.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.