← Questions

Optimize a small-string class in C++

0
NvidiaCodingLast asked Dec 2025

A coding interview question asked in Nvidia's coding round, last asked Dec 2025. It tests c++, memory management and performance.

You are building a high-performance C++ string type for a workload with a very large number of strings. It uses a small-string optimization: short strings live in a fixed buffer inside the object, long strings on the heap. Start from this simplified layout and answer the questions below; expect to write and fix code as you go.

const size_t BUFF_SIZE = 128;

class MyString {
private:
  char   buf[BUFF_SIZE];  // inline storage for short strings
  size_t length;          // bytes, not counting '\0'
  char*  ptr;             // heap storage 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. strncpy copies byte by byte and stops at a '\0'. How would you make the small-string copy faster?
  2. Is memcpy(buf, s, len) equivalent to strncpy(buf, s, len)? Explain the behavioural differences and the safety pitfalls of each.
  3. In a compare function, why can comparing short strings (under 256 bytes, say) be much faster than comparing long ones, beyond the obvious difference in length?
  4. If BUFF_SIZE == 1, what is the likely sizeof(MyString) on a 32-bit and on a 64-bit machine? Explain alignment and padding.
  5. If BUFF_SIZE == 8 but typical strings are 10–15 characters, how would you redesign the layout to shrink the object and improve cache locality?
  6. Write the destructor and the copy and move operations, and find the bugs in the constructor as given.

What a strong answer covers

  • Fast copies: memcpy of a known length (or a fixed-size copy of the whole small buffer), letting the compiler emit wide moves.
  • strncpy versus memcpy: strncpy stops at embedded '\0', pads with zeros and does not terminate when the limit is reached; memcpy copies exactly len bytes and must not overlap.
  • Short compares: data in the object's own cache lines, no pointer chase to the heap, early mismatch, and SIMD-friendly fixed sizes.
  • Padding: with BUFF_SIZE == 1, 12 bytes on a typical 32-bit target and 24 on 64-bit, because size_t and the pointer are aligned to their size.
  • A union of the inline buffer and the heap pointer plus capacity, with a flag bit or the length to tell them apart, so short strings need no separate pointer (libc++, for example, keeps up to 22 characters inline in a 24-byte object).
  • Constructor bugs: ptr left uninitialized for short strings, throwing a string literal, malloc paired with C++ ownership; a destructor that frees only the heap case; copy and move that handle both modes and self-assignment.

Follow-ups

  • Implement concatenation that avoids allocation when the result still fits inline.
  • How do you make substring cheap without copying?
  • Measure whether your change helped: what benchmark would you write?

Where it's asked

  • Nvidia · Coding · Last asked Dec 2025Live coding, often C++-leaning; each round usually opens with 10 to 15 minutes on your past work · Assume no AI unless the interviewer offers it

What it tests

c++ · memory management · performance

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding