9Why can an empty C++ vector object occupy memory even when its size and capacity are zero?
The vector object and its dynamically allocated element storage are different things. Even when size() and capacity() are both zero, the object must represent its state and support future operations. A common implementation stores begin, end, and end-of-storage pointers, possibly together with allocator or debugging state. Therefore sizeof(vector<T>) is nonzero even when no element buffer has been allocated. The standard does not require a specific number of pointers or a fixed byte size, so architecture, library, allocator type, and build mode can change it. Also, an empty vector can have nonzero capacity after reserve or clear, in which case it may retain an allocation as well. Distinguish the control object's footprint from live elements and reserved storage when measuring memory.
Try answering this question with an AI coach
10How do a stack and an ordinary queue differ, and which kind of task naturally fits each one?
A stack removes the most recently added item first: last in, first out. Its core operations are push, pop and peek at the top. This naturally fits nested work such as parsing balanced delimiters, depth-first traversal or undoing the latest action. An ordinary queue removes the oldest waiting item first: first in, first out, using enqueue at the rear and dequeue at the front. It fits breadth-first traversal or serving work in arrival order. Both can support constant-time end operations with suitable arrays, ring buffers or linked structures; implementation choice affects capacity and memory locality. A priority queue is different because removal follows priority rather than arrival order. Neither basic ordering rule alone guarantees thread safety, bounded memory or fair completion when processing times differ.
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.
// Exact lookup, frequently updated:
std::unordered_map<std::string, User> users_by_id;
// Need sorted iteration or lower_bound/range queries:
std::map<std::string, User> users_by_id_ordered;
// Build once, query many times: vector sorted by key is a common flat-map style.
std::vector<std::pair<std::string, User>> users;
std::sort(users.begin(), users.end(), [](auto const& a, auto const& b) {
return a.first < b.first;
});
auto it = std::lower_bound(users.begin(), users.end(), std::string_view{"u123"},
[](auto const& p, std::string_view key) { return p.first < key; });
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.
struct UserProfile {
std::string id;
std::optional<std::string> display_name; // absent if user has not set it
};
std::string label(UserProfile const& u) {
return u.display_name.value_or("anonymous");
}
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.
std::string_view bad() {
std::string s = "hello";
return std::string_view{s}; // dangling after return
}
void ok(std::string_view name) {
// safe only during this call if caller's data outlives the call
}
Try answering this question with an AI coach