Implement Copy-To and Copy-From Operations on a Fixed-Capacity Byte Buffer

Quick Overview

A low-level coding exercise to implement copy-to and copy-from operations on a fixed-capacity byte buffer. It tests interface design, overflow-safe bounds checking, overlapping memory, a consistent partial-copy policy and thread safety, with a circular-buffer follow-up.

Implement Copy-To and Copy-From Operations on a Fixed-Capacity Byte Buffer

Company: Crusoe

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Onsite

In a coding round for a role that rewards low-level systems experience, you are asked to write two operations on a byte buffer: copy to the buffer (copy bytes from the caller's memory into the buffer) and copy from the buffer (copy bytes out of the buffer into the caller's memory). The report names only these two operations, so agree on the interface and the error behavior with the interviewer before coding. A C interface such as this one is a reasonable starting point: ```c struct buffer { uint8_t *data; /* storage owned by the buffer */ size_t capacity; /* size of data in bytes */ }; /* Copy n bytes from src into the buffer, starting at byte `offset` of the buffer. */ ssize_t copy_to_buffer(struct buffer *buf, size_t offset, const void *src, size_t n); /* Copy n bytes out of the buffer, starting at byte `offset`, into dst. */ ssize_t copy_from_buffer(const struct buffer *buf, size_t offset, void *dst, size_t n); ``` Implement both operations, explain how each behaves on every invalid or borderline request, and say how you would test them. ```hint Check the range before touching memory Ask what your bounds check computes when `offset` and `n` are both close to the largest value a `size_t` can hold. ``` ```hint Where the caller's pointer points Ask what happens if the caller passes a pointer into the buffer's own storage. ``` ### Clarifying Questions - Is the buffer a flat array addressed by offset, or a circular queue in which copy-to appends and copy-from consumes? - If a request runs past the end of the buffer, should the call fail without copying anything, or copy what fits and report how much it copied? - How are errors reported: a negative error code, a count of bytes not copied, or something else? - Can the caller's memory overlap the buffer's storage? - Can several threads use the same buffer at the same time? - Can the caller's memory come from a less trusted context, such as a user process, so that reading or writing it can fault? ### What a Strong Answer Covers - An agreed interface: argument order, return value, and how errors are reported - Bounds checks that cannot overflow, plus null pointers and zero-length copies - Correct results when the source and the destination overlap - One partial-copy policy, applied the same way by both operations - Tests at the boundaries: a copy that ends exactly at capacity, one byte past it, zero bytes, and huge offsets - Thread safety and, for untrusted memory, fault handling ### Follow-up Questions - How would both operations change if the buffer were a circular queue with a read position and a write position? - How would you make that circular version safe for one producer thread and one consumer thread without a lock? - If the source pointer came from an untrusted process, what extra checks and fault handling would the copy need? - How would you make large copies fast, and when does alignment matter?

Overview: A low-level coding exercise to implement copy-to and copy-from operations on a fixed-capacity byte buffer. It tests interface design, overflow-safe bounds checking, overlapping memory, a consistent partial-copy policy and thread safety, with a circular-buffer follow-up.

Crusoe logo
Crusoe
Oct 29, 2026
easySoftware EngineerOnsiteSoftware Engineering Fundamentals
0
0

In a coding round for a role that rewards low-level systems experience, you are asked to write two operations on a byte buffer: copy to the buffer (copy bytes from the caller's memory into the buffer) and copy from the buffer (copy bytes out of the buffer into the caller's memory). The report names only these two operations, so agree on the interface and the error behavior with the interviewer before coding. A C interface such as this one is a reasonable starting point:

struct buffer {
    uint8_t *data;      /* storage owned by the buffer */
    size_t   capacity;  /* size of data in bytes */
};

/* Copy n bytes from src into the buffer, starting at byte `offset` of the buffer. */
ssize_t copy_to_buffer(struct buffer *buf, size_t offset, const void *src, size_t n);

/* Copy n bytes out of the buffer, starting at byte `offset`, into dst. */
ssize_t copy_from_buffer(const struct buffer *buf, size_t offset, void *dst, size_t n);

Implement both operations, explain how each behaves on every invalid or borderline request, and say how you would test them.

Clarifying Questions Guidance

  • Is the buffer a flat array addressed by offset, or a circular queue in which copy-to appends and copy-from consumes?
  • If a request runs past the end of the buffer, should the call fail without copying anything, or copy what fits and report how much it copied?
  • How are errors reported: a negative error code, a count of bytes not copied, or something else?
  • Can the caller's memory overlap the buffer's storage?
  • Can several threads use the same buffer at the same time?
  • Can the caller's memory come from a less trusted context, such as a user process, so that reading or writing it can fault?

What a Strong Answer Covers Guidance

  • An agreed interface: argument order, return value, and how errors are reported
  • Bounds checks that cannot overflow, plus null pointers and zero-length copies
  • Correct results when the source and the destination overlap
  • One partial-copy policy, applied the same way by both operations
  • Tests at the boundaries: a copy that ends exactly at capacity, one byte past it, zero bytes, and huge offsets
  • Thread safety and, for untrusted memory, fault handling

Follow-up Questions Guidance

  • How would both operations change if the buffer were a circular queue with a read position and a write position?
  • How would you make that circular version safe for one producer thread and one consumer thread without a lock?
  • If the source pointer came from an untrusted process, what extra checks and fault handling would the copy need?
  • How would you make large copies fast, and when does alignment matter?