Explain C++ memory, types, and concurrency fundamentals
Company: Sunrise
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Onsite
Answer the following C++ / systems fundamentals questions:
1) **C++ `class` vs `struct`**
- What are the differences between `class` and `struct` in C++?
- Can a `class` have a reference (`T&`) as a data member? What are the implications?
2) **Virtual constructor**
- Can a constructor be `virtual` in C++? If not, what patterns achieve “polymorphic construction”?
3) **Pointer vs reference**
- What are the key differences between pointers and references in C++ (nullability, reseating, object lifetime, ABI, usage)?
4) **Placement new**
- What is placement `new`? When would you use it, and what must you do manually after using it?
5) **Performance comparison (cache/memory effects)**
- Compare likely performance and CPU/memory behavior of these two loops (assume `data` points to a large `int` array of length `64*1024*1024` allocated on the heap):
- Code A: increment `data[0]` in a tight loop `N` times.
- Code B: increment `data[i]` for `i = 0; i < N; i += 8`.
- Discuss caching, prefetching, write allocation, store buffers, memory bandwidth, and why one might be faster.
6) **Function pointer type**
- In `int (*s[10])(int);`, what is the type of `s`?
7) **`unordered_map` vs `map`**
- Compare complexity guarantees, ordering, iterator invalidation, memory overhead, and typical use cases.
8) **Overloading vs overriding**
- Explain function overloading vs method overriding (virtual dispatch) in C++.
9) **Mutex vs condition variable**
- What problems does each solve? Why do condition variables require a mutex and a loop around `wait`?
10) **What causes a physical memory access?**
- At a high level, what events force the CPU to access DRAM (vs cache)? Include page faults, TLB misses, cache misses, and DMA.
11) **Shared memory synchronization**
- If multiple processes/threads communicate via shared memory, how do you correctly synchronize access? Discuss options (process-shared mutexes, semaphores, futexes, file locks) and pitfalls.
12) **Linux networking and virtualization (high level)**
- Describe how a packet travels through the Linux networking stack, and how common virtualization primitives (namespaces, veth pairs, bridges, TAP, iptables/nftables) fit into that path.
Quick Answer: Evaluates understanding of C++ language- and systems-level fundamentals—including type and object-model distinctions (class vs struct, pointers vs references, overloading vs overriding), construction and placement new idioms, function pointer syntax, container complexity, memory and cache behavior, synchronization primitives (mutexes, condition variables, futexes, semaphores), causes of physical memory access, and high-level Linux networking and virtualization packet flow. Commonly asked in Software Engineering Fundamentals interviews because it probes low-level language semantics and OS interactions essential for correct concurrency, performance reasoning, and system integration; category/domain: Software Engineering Fundamentals; level of abstraction: language- and OS-level systems fundamentals.