← Questions

Design a string type: memory layout, moves and thread safety

0
SpaceXAICodingLast asked Jul 2025

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]) -> int with these operations on named variables:
    • make name len — a new string of length len, capacity the smallest power of two >= max(1, len).
    • copy src dst — a deep copy: copies len(src) bytes; dst has that length and capacity max(1, len).
    • move src dst — no bytes copied; dst takes src's length and capacity, and src becomes invalid.
    • append name k — if len + k exceeds capacity, double capacity until it fits, copying the old len bytes once; then len += 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^5 operations; lengths up to 10^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

  • String is {ptr, len, cap} (24 bytes on 64-bit) owning a UTF-8 heap buffer; &str is 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 String can be sent between threads; sharing needs Arc<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 String by character position in O(1)?
  • Implement copy-on-write strings and say where they hurt.

Where it's asked

What it tests

rust · memory · concurrency · data structures

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