Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Explain Existing Code Behavior

MediumSecurity & Infrastructure00:00
Practice interviewer
In session
5 left
00:00

Your question is Explain Existing Code Behavior. Take a moment with it on the right.

Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).

You need to log in / sign up to chat or submit.

Problem

At Qualcomm, you're pairing with a firmware engineer on a thermal-management module for a mobile SoC. They hand you the snippet below, pulled from the sensor polling task, and ask you to read it cold before they explain any of it.

#define TEMP_REG        0x02
#define THRESHOLD_C     75
#define SAMPLE_WINDOW   8

static int16_t sample_buffer[SAMPLE_WINDOW];
static uint8_t sample_index = 0;
static uint8_t alert_active = 0;

int16_t read_temp_register(uint8_t addr) {
    uint8_t raw = i2c_read_byte(addr, TEMP_REG);
    return (int16_t)(raw * 0.5f) - 40;
}

void push_sample(int16_t value) {
    sample_buffer[sample_index] = value;
    sample_index = (sample_index + 1) % SAMPLE_WINDOW;
}

int16_t average_temp(void) {
    int32_t sum = 0;
    for (uint8_t i = 0; i < SAMPLE_WINDOW; i++) {
        sum += sample_buffer[i];
    }
    return (int16_t)(sum / SAMPLE_WINDOW);
}

void temp_monitor_tick(void) {
    int16_t t = read_temp_register(0x48);
    push_sample(t);
    int16_t avg = average_temp();

    if (avg >= THRESHOLD_C && !alert_active) {
        alert_active = 1;
        raise_thermal_alert();
    } else if (avg < THRESHOLD_C - 5 && alert_active) {
        alert_active = 0;
        clear_thermal_alert();
    }
}

Look at the code and, in your own words, explain what each function does and what the overall program (assume temp_monitor_tick is called once per scheduler tick) is accomplishing.