Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
Spot and Fix C Memory Bugs
00:00
5 left

Spot and Fix C Memory Bugs

HardPython

Problem

Arm's firmware team assembles an incoming firmware update image from a sequence of binary chunks received over the wire. QA has reported both crashes during long-running update sessions and steadily rising memory usage across repeated updates.

#include <stdlib.h>
#include <string.h>

typedef struct {
    char *buffer;
    size_t size;
} FirmwareImage;

FirmwareImage *load_firmware_chunk(const char *chunk, size_t chunk_len) {
    FirmwareImage *image = malloc(sizeof(FirmwareImage));
    image->buffer = malloc(chunk_len);
    strcpy(image->buffer, chunk);
    image->size = chunk_len;
    return image;
}

void append_chunk(FirmwareImage *image, const char *chunk, size_t chunk_len) {
    char *new_buffer = malloc(image->size + chunk_len);
    memcpy(new_buffer, image->buffer, image->size);
    memcpy(new_buffer + image->size, chunk, chunk_len);
    image->buffer = new_buffer;
    image->size += chunk_len;
}

void free_firmware(FirmwareImage *image) {
    free(image);
}

Spot the errors in this C program, including memory leaks and buffer overflows, and explain how you would correct them. Chunks are raw binary data, not text, and may contain embedded zero bytes or lack any terminating zero byte at all.

Constraints

  • 0 <= chunk_len <= len(chunk) <= 10^4
  • Each element of chunk is an integer in the range 0 to 255
  • chunk represents raw binary data, not a null-terminated string

Function Signature

def fix_firmware_image(chunk, chunk_len):
Interviewer

Your question is Spot and Fix C Memory Bugs. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.