Understanding C++ vector memory allocation and resizing
This is a medium-difficulty C++ language-knowledge question that tests your grasp of how standard library containers manage dynamic memory. It requires you to trace through vector growth behaviour and predict the output of a snippet that likely exercises capacity changes as elements are added.
Quant firms ask questions like this to verify that candidates understand the difference between a vector's size (number of elements) and capacity (allocated memory), and how the STL's reallocation strategy affects pointers and references. Getting this right means you can reason reliably about memory layout and object lifetime in performance-critical code.
- Vector size vs. capacity semantics
- Reallocation triggers and growth factors
- Validity of pointers and iterators after resizing