Design a string type: memory layout, moves and thread safety
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in SpaceXAI's technical phone screen, last asked Jul 2025. It tests rust, memory and concurrency.
What is a string in a programming language, and how would you build one? Describe the fields a growable string holds (Rust's String as the reference point), how you would implement it yourself, what copying a string costs, and how move semantics avoid that cost. Is a move in Rust simply a reference update? Then implement a small simulation that counts the bytes a string type copies.
Part 1: explain
- What a string is: a sequence of bytes in some encoding (UTF-8 in Rust) plus a length, as opposed to a C string ended by a zero byte.
- The fields of a growable string: pointer to a heap buffer, length, capacity.
- What a copy (
clone) costs, and what a move does in Rust.
Part 2: count copied bytes
Unless you agree on a different interface with the interviewer, assume:
-
bytes_copied(ops: list[str]) -> intwith these operations on named variables:-
make name len— a new string of lengthlen, capacity the smallest power of two>= max(1, len). -
copy src dst— a deep copy: copieslen(src)bytes;dsthas that length and capacitymax(1, len). -
move src dst— no bytes copied;dsttakessrc's length and capacity, andsrcbecomes invalid. -
append name k— iflen + kexceeds capacity, double capacity until it fits, copying the oldlenbytes once; thenlen += k.
-
- Only deep copies and reallocation copies count, not the appended data itself.
bytes_copied(["make a 5", "copy a b", "append b 1", "move a c", "append c 4"]) -> 15
# copy: 5; b grows from cap 5 to 10: 5; c (cap 8) grows to 16: 5
bytes_copied(["make a 5", "append a 3", "append a 1"]) -> 8
# cap 8 holds 8 bytes; the ninth forces a copy of 8
Constraints
- Up to
2 * 10^5operations; lengths up to10^9; operations are always valid.
Clarifying questions
- Which language's semantics: Rust, C++, or a garbage-collected one?
- Is the string mutable?
- Are small strings stored inline (small-string optimization)?
- Is thread safety in scope?
What a strong answer covers
-
Stringis{ptr, len, cap}(24 bytes on 64-bit) owning a UTF-8 heap buffer;&stris a borrowed{ptr, len}. - Copying is O(n) in the length: a new allocation and a byte copy. Amortized O(1) append from geometric growth.
- A Rust move copies the three-word header bitwise and makes the source unusable at compile time; no heap bytes move and no reference count changes, so it isn't a "reference update" but a shallow copy plus ownership transfer. C++ moves need a move constructor that nulls the source.
- Ways to make it cheaper: reserving capacity, small-string optimization, copy-on-write or reference-counted immutable strings.
- Thread safety: an owned
Stringcan be sent between threads; sharing needsArc<str>for reads or a lock for mutation. - For Part 2, a map from name to
(len, cap, alive)and O(log) doubling per append.
Follow-ups
- Add small-string optimization: what changes in the layout?
- Why can't you index a Rust
Stringby character position in O(1)? - Implement copy-on-write strings and say where they hurt.
Where it's asked
- SpaceXAI · Technical phone screen · Last asked Jul 202545 min · Live applied coding on a shared platform such as CoderPad · No AI
What it tests
rust · memory · concurrency · data structures
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.
