← Questions
Optimize a small-string class in C++
0
I was asked this
No reports yet. Sign in to say where you were asked it.
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
-
strncpycopies byte by byte and stops at a'\0'. How would you make the small-string copy faster? - Is
memcpy(buf, s, len)equivalent tostrncpy(buf, s, len)? Explain the behavioural differences and the safety pitfalls of each. - 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?
- If
BUFF_SIZE == 1, what is the likelysizeof(MyString)on a 32-bit and on a 64-bit machine? Explain alignment and padding. - If
BUFF_SIZE == 8but typical strings are 10–15 characters, how would you redesign the layout to shrink the object and improve cache locality? - 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:
memcpyof a known length (or a fixed-size copy of the whole small buffer), letting the compiler emit wide moves. -
strncpyversusmemcpy:strncpystops at embedded'\0', pads with zeros and does not terminate when the limit is reached;memcpycopies exactlylenbytes 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, becausesize_tand 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:
ptrleft uninitialized for short strings, throwing a string literal,mallocpaired 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
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
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
