NVIDIA · CS Fundamentals
Optimize a small-string C++ class
TrueInterview
October 7, 2026 · 1 min read
You are building a compact C/C++ string class with small-string optimization: short payloads live inside the object itself, while long payloads live in a dynamically allocated buffer.
Use this simplified class arrangement as the basis for the questions below:
const size_t INLINE_CAP = 128;
class CompactString {
private:
char inlineBuf[INLINE_CAP]; // storage for short strings
size_t byteCount; // number of bytes, excluding the terminator
char* heapBuf; // storage for long strings
public:
CompactString(const char* src, size_t n) {
byteCount = n;
if (n < INLINE_CAP) {
strncpy(inlineBuf, src, n);
inlineBuf[n] = '\0';
} else {
heapBuf = (char*)malloc(n + 1);
if (heapBuf == nullptr) throw "out of memory";
memcpy(heapBuf, src, n);
heapBuf[n] = '\0';
}
}
};
-
The call
strncpy(inlineBuf, src, n)performs a byte-by-byte copy in principle. What technique would improve copy throughput for the inline/short case? -
Does replacing that call with
memcpy(inlineBuf, src, n)change behavior? If yes, list the differences and any safety hazards. -
In a comparison routine, why might strings shorter than 256 bytes compare much faster than longer strings, even if the length gap is disregarded conceptually?
-
If
INLINE_CAP == 1, what is the likelysizeof(CompactString)on a 32-bit platform and on a 64-bit platform? Discuss alignment and padding. -
Suppose
INLINE_CAP == 8while most strings are about 10–15 characters. How could the representation be redesigned to shrink objects and improve cache locality? (Hint: do not simultaneously reserve inline space and a pointer if only one is needed.)
This exercise assesses low-level C/C++ systems programming ability, including memory management, data layout, small-string optimization, efficient copying semantics, alignment/padding, and cache-aware performance reasoning.
Examples
Example 1
Input: 1 8
Output: 24 8 8 16 7
Example 2
Input: 1 4
Output: 12 4 4 8 3