Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
Correct Sensor Buffer and Loop Errors
00:00
5 left

Correct Sensor Buffer and Loop Errors

HardPython

Problem

You're a software engineer at Arm working on firmware that reads sensor values on a Cortex-M microcontroller. This C code has been flagged in review as unsafe for a long-running embedded device.

#include <stdio.h>
#include <stdlib.h>

#define SENSOR_COUNT 8

int *readings;

void read_sensors(int *raw_values, int count) {
    readings = malloc(count * sizeof(int));

    for (int i = 0; i <= count; i++) {
        readings[i] = raw_values[i];
    }
}

int find_max_reading(int count) {
    int max = 0;
    for (unsigned int i = count - 1; i >= 0; i--) {
        if (readings[i] > max) {
            max = readings[i];
        }
    }
    return max;
}

void report(int raw_values[SENSOR_COUNT]) {
    read_sensors(raw_values, SENSOR_COUNT);
    printf("max reading: %d
", find_max_reading(SENSOR_COUNT));
}

How would you rewrite the given C code? Walk through every safety and correctness problem you find and explain your approach to fixing each one.

Write a safe Python version of the logic that returns the maximum sensor reading from a fixed-size list of readings, and explain the bugs in the original code.

Constraints

  • raw_values contains exactly 8 integers
  • -10^9 <= raw_values[i] <= 10^9
  • The function should run in O(n) time
  • Do not use global mutable state

Function Signature

def max_sensor_reading(raw_values):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.