Interview Questions
Arrays and Vectors · Study material
C++ Arrays and Vectors – Interview Questions
1. What is the difference between an array and a vector?
A C array has a compile-time fixed size, lives on the stack (as a local), decays to a pointer and cannot be copied or returned. A vector has a run-time size, stores its elements in a heap buffer, manages its memory and can be copied, moved and returned.
2. What does "array decay" mean?
The automatic conversion of an array to a pointer to its first element in most expressions, including when passed to a function. The size information is lost.
3. How are 2D arrays stored in memory?
In row-major order: all elements of row 0, then row 1, and so on. Element [r][c] of an R × C array is at offset r × C + c.
4. What is the difference between size() and capacity()?
size is the number of elements; capacity is how many elements the allocated buffer can hold before the vector must reallocate.
5. How does a vector grow?
When full, it allocates a larger buffer (typically 1.5× or 2× the old capacity), moves or copies the elements and frees the old buffer. This gives amortized O(1) push_back.
6. What is the difference between reserve and resize?
reserve(n) only guarantees capacity ≥ n; the size is unchanged. resize(n) changes the number of elements, value-initializing new ones or destroying extras.
7. push_back vs emplace_back?
push_back takes an existing object and copies or moves it in; emplace_back takes constructor arguments and constructs the element directly in the vector.
8. What is the difference between v[i] and v.at(i)?
at checks the index and throws std::out_of_range if invalid; operator[] does not check, and an invalid index is undefined behaviour.
9. What is iterator invalidation?
After operations that reallocate (push_back beyond capacity, reserve, insert) all iterators, pointers and references to elements become invalid; erase invalidates those at and after the erased position.
10. Explain the erase-remove idiom.
std::remove moves the elements to keep to the front and returns the new logical end; vector::erase then deletes the leftover tail. C++20 replaces it with std::erase and std::erase_if.
11. Does clear() free a vector's memory?
No – it destroys the elements and sets size to 0 but usually keeps the capacity. shrink_to_fit requests a release, or swap with an empty vector.
12. Why is std::vector<bool> special?
It is specialised to store bits, so its elements are not real bool objects: operator[] returns a proxy and you cannot take a bool* to an element.
13. What advantages does std::array have over a C array?
It knows its size, does not decay, can be copied, compared and returned, works with algorithms and offers at() – with no runtime overhead.
14. How do you pass a 2D array to a function?
The column count must be part of the type: void f(int m[][4], int rows). Easier alternatives are std::array of std::array, std::vector of vectors or a flat vector with dimensions.
15. Are vector elements stored contiguously?
Yes. data() returns a pointer to the first element, so a vector can be passed to C APIs expecting an array.
16. What is std::span?
A C++20 non-owning view of a contiguous sequence (pointer + length). It lets one function accept arrays, std::array and vectors without copying.
17. What is the time complexity of inserting in the middle of a vector?
O(n) – all following elements must be shifted. Inserting or removing at the end is amortized O(1).
18. Can a C array's size be a variable?
Not in standard C++. Variable length arrays are a C feature some compilers accept as an extension; use std::vector instead.
19. What does vector<int> v(5); contain?
Five value-initialized ints, i.e. five zeros. vector<int> v{5}; contains a single element, 5.
20. How do you find the maximum element and its index?
auto it = std::max_element(v.begin(), v.end()); the value is *it and the index is it − v.begin().