Explain and Implement Strings
Company: xAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates understanding of string data structures, memory layout, ownership and move semantics in Rust, and algorithmic complexity related to copying and performance.
Constraints
- 1 <= len(ops) <= 200000
- 0 <= len, k <= 10^9
- All operations are valid as per the rules (e.g., src exists, dst does not yet exist; moved-from variables are not used until re-created).
- Capacity doubling is by repeated multiplication by 2 until capacity >= required length.
- Bytes counted are only from deep copies (copy) and internal reallocations during append; appending new external data itself does not count.
Hints
- Track for each variable its (length, capacity) and whether it is alive.
- For append that exceeds capacity, add the old length once to the total and then double capacity until sufficient.
- copy adds exactly src.length to the total; move adds 0 and invalidates the source.