KatInterview

String Internals, Move Costs, and Threads vs Async Tasks

StringsModel answer
MediumModel answerStringsstringsmemory-managementmove-semantics

Last updated

Describe how a string is laid out in memory, what copying and moving really cost, what a Rust move does, and how OS threads differ from async coroutines.

This is a fundamentals discussion. Answer each part concretely, using memory layouts, byte counts and big-O costs where they help.

  1. What is a string? Explain how mainstream languages represent one (for example C, C++, Rust, Java, Python), and what separates mutable from immutable designs.
  2. Layout. Which fields would a typical owning, growable string struct hold? Describe how you would implement one yourself, including growth policy, copy behavior and cleanup.
  3. Copy cost. What is the time complexity of copying a string? Do the answer for several designs: deep copy, short-string optimization, immutable or reference-counted strings, and copy-on-write.
  4. Efficient moves. What is a move, and what can you do so that moves stay cheap in practice?
  5. Rust specifically. A colleague says: "In Rust, moving a String just changes the reference." Is that right? Describe what really happens to the value, the heap buffer, and the source variable.
  6. Threads vs. async coroutines. Compare them on scheduling, stack usage, switch cost, scalability, parallelism, suitable workloads and typical failure modes.

A strong answer is structured, gives numbers (such as struct size on a 64-bit machine), and flags common pitfalls.

Hints

Hint 1

Think about what an owning string must track in order to find its data, know where it ends, and append without reallocating every time. Count the machine words.

Hint 2

Separate what is copied (the small inline struct) from what is not touched (the heap buffer). Then ask what the compiler does with the source variable afterwards, and whether a runtime reset is needed.

Hint 3

For threads vs. coroutines, distinguish concurrency from parallelism, then compare who decides when to switch (kernel vs. the program itself) and what happens when one task blocks.