KatInterview

Design a Small-String-Optimized Buffer in C++

System DesignModel answer
MediumModel answerSystem DesignC++memory-managementsmall-string-optimization

Last updated

Evaluate low-level C++ skills: memory layout, copy semantics, alignment/padding, and how SSO eliminates heap allocations for short strings.

Background

Many real-world C++ runtimes avoid heap allocations for short strings by storing the characters inline inside the object itself. This is called small-string optimization (SSO). Below is a simplified starting point that demonstrates the idea but leaves several performance and correctness problems on the table.

const size_t BUFF_SIZE = 128;

class MyString {
private:
    char   buf[BUFF_SIZE]; // inline storage for short strings
    size_t length;         // character count, not including the null terminator
    char*  ptr;            // heap pointer for long strings

public:
    MyString(const char* s, size_t len) {
        length = len;
        if (len < BUFF_SIZE) {
            strncpy(buf, s, len);
            buf[len] = '\0';
        } else {
            ptr = (char*)malloc(len + 1);
            if (ptr == nullptr) throw "not enough memory";
            memcpy(ptr, s, len);
            ptr[len] = '\0';
        }
    }
};

Questions

  1. Copy speed. strncpy(buf, s, len) copies characters in a byte-by-byte loop. What would you use instead, and why is it faster for short strings?

  2. memcpy vs strncpy. Switching to memcpy(buf, s, len) is not a drop-in replacement. What are the behavioral differences, and what safety pitfalls does each introduce?

  3. Comparison performance. In a string-comparison function, why can comparing short strings (under 256 bytes) be significantly faster than comparing long ones — even if you set aside the fact that they have fewer characters?

  4. Struct layout. If BUFF_SIZE is changed to 1, what is sizeof(MyString) on a 32-bit machine and on a 64-bit machine? Walk through how the compiler derives each number.

  5. Layout redesign. BUFF_SIZE = 8 but typical strings in your workload are 10–15 characters, so the inline buffer is almost never useful. How would you restructure the class so that one object can serve either role without paying for both at the same time?

A strong answer addresses each point with concrete reasoning. Where relevant, mention correctness issues you notice in the existing code.

Hints

Hint 1

For question 1, think about what extra work strncpy does beyond moving bytes, and what CPU feature lets memcpy skip that work entirely.

Hint 2

For question 4, start by writing out each member's size and its alignment requirement, then apply the two rules: each member starts at a multiple of its own alignment, and the total struct size rounds up to a multiple of the largest alignment.

Hint 3

For question 5, a small string and a heap-allocated string are mutually exclusive states — that is a strong hint that a union can overlap their storage, with a single flag byte distinguishing the two modes.