9How does std::vector manage capacity, growth, reallocation, and iterator stability?
std::vector stores elements contiguously and tracks both size and capacity. size is the number of constructed elements; capacity is the amount of allocated element storage available before another allocation is needed. When adding elements would exceed capacity, vector allocates a larger block, typically using an implementation-defined geometric growth strategy, moves or copies existing elements, destroys the old ones, and releases the old storage. reserve(n) increases capacity without changing size, while resize(n) changes size by constructing or destroying elements. Reallocation invalidates all iterators, references, and pointers to elements; even without reallocation, operations such as insert and erase can invalidate positions at or after the modification point.
Try answering this question with an AI coach
10What invalidation rules should you know for contiguous and node-based standard containers?
Invalidation rules depend on the container and the operation. Contiguous containers such as vector and string have fragile iterator/reference stability: growth may reallocate and invalidate all iterators, references, and pointers, and insert/erase can shift elements and invalidate positions at or after the change even without reallocation. Node-based ordered containers such as list, map, set, and their multi variants generally keep iterators and references to existing non-erased elements stable across insert; erasing an element invalidates the iterator/reference to that erased element. Unordered containers also store elements in nodes, so references and pointers to elements are generally stable across rehash, but rehash invalidates iterators. deque has special segmented-storage rules. In practice, check the specific container and operation before storing iterators or references across modifications.
Try answering this question with an AI coach
11Compare std::map, std::unordered_map, and flat-map-style containers for backend lookup tables.
std::map is an ordered, usually tree-based associative container with logarithmic lookup/insert/erase; it is useful when sorted iteration, range queries, or ordering guarantees matter. std::unordered_map is hash-table based with average constant-time exact-key operations and no key ordering; it is often a good default for large mutable lookup tables when hashing is good. A flat-map-style container stores sorted key/value pairs contiguously, giving good cache locality and fast iteration/binary-search lookup, but insertion and erasure in the middle are linear. For backend lookup tables, choose based on whether the workload needs ordering/ranges, mostly exact lookups, frequent mutation, predictable latency, memory overhead, and cache behavior.
Try answering this question with an AI coach
12Explain std::optional and typical backend use cases for representing absent values.
std::optional<T> represents either a contained T value or no value. The empty state is represented by std::nullopt; code can check has_value() or use the optional in a boolean context, access the value with * or value(), and supply a default with value_or(). In backend code it is useful for nullable database fields, optional request/configuration fields, cache or repository misses where absence is expected, and domain states where a sentinel like -1 or an empty string would be ambiguous. It models absence of a value, not polymorphism or rich error information.
Try answering this question with an AI coach
13What are std::string_view and std::span, and what lifetime hazards do non-owning views introduce?
std::string_view is a non-owning view of a contiguous character sequence; std::span<T> is a non-owning view of a contiguous sequence of T. They are useful for zero-copy parameters and buffer APIs because they carry a pointer and a length without allocating or owning memory. The main hazard is lifetime: the referenced storage must outlive the view and must not be invalidated while the view is used. Returning or storing a view to a temporary, local object, destroyed object, or reallocated container can leave a dangling view.
Try answering this question with an AI coach