Whenever you're ready, walk me through how you'd approach this.
Alten's automotive team asked a candidate to read through this C function, taken from a simplified engine-control sample, and explain what it does before diving into the rest of the interview.
#include <stdio.h>
int compute_average_rpm(int *readings, int count) {
int total = 0;
for (int i = 0; i <= count; i++) {
total = total + readings[i];
}
return total / count;
}
void log_reading(int rpm) {
char buffer[8];
sprintf(buffer, "RPM=%d", rpm);
printf("%s
", buffer);
}
int main() {
int readings[5] = {1200, 1250, 1300, 4200, 1225};
int avg = compute_average_rpm(readings, 5);
log_reading(avg);
return 0;
}
Explain what this code does, then identify anything about it you'd flag as a bug before it went anywhere near a real engine-control unit.