C++ Interview Review: OOP Concepts, STL Usage, Networking and OS Fundamentals
This article reviews a C++ interview covering object‑oriented programming basics, STL container behaviors, networking protocols, inter‑process communication mechanisms, memory layout, and common interview questions, providing concise explanations and code examples for each topic.
C++ Related
Understanding Object‑Oriented Programming
C++ OOP treats everything as objects, describing them with attributes and methods; for example, a cat object has eyes, fur, mouth as attributes and meow, walk as methods.
Programming with objects improves code reusability and maintainability.
The three major OOP features in C++ are encapsulation, inheritance, and polymorphism.
Encapsulation : combines data (variables) and functions that operate on the data into a single "object" and hides internal details.
Inheritance : creates a new class from an existing one, inheriting its features while adding new ones.
Polymorphism : allows different types to be accessed through a common interface, typically implemented via virtual functions.
Difference Between Normal Functions and Member Functions
Normal functions are defined outside a class; member functions are defined inside a class.
Normal functions cannot directly access private or protected members; member functions can.
Normal functions are called directly; member functions require an object.
Member functions have an implicit this pointer; normal functions do not.
What Is the this Pointer?
The this pointer holds the address of the current object and is used inside member functions to access the object's members.
It is passed implicitly by the compiler when a non‑static member function is invoked.
Static member functions do not have a this pointer because they are not tied to any object.
Purpose of the static Keyword
staticcan declare static variables (local, global, or class static members) and static member functions.
Local static variable: stored in static storage, initialized once, scope limited to the block.
Global static variable: lives for the program lifetime, visible only within its translation unit.
Class static member variable: shared by all objects of the class, must be defined outside the class.
Class static member function: shared by all objects, has no this, cannot access non‑static members.
Vector Expansion, resize vs reserve
When a vector runs out of space, it expands; Visual Studio expands by 1.5×, GCC by 2×.
resize(n) : changes the vector size to contain n elements, adding default‑constructed elements if n is larger, or removing elements if smaller.
reserve(n) : does not change the size; it pre‑allocates memory for at least n elements, affecting capacity() but not size(). It is used to improve performance by reducing reallocations.
In short, resize changes the number of elements, while reserve changes the allocated capacity.
push_back vs emplace_back
emplace_backusually outperforms push_back because it constructs the element directly in place, avoiding unnecessary copy or move operations.
push_back : creates a temporary object, then copies or moves it into the container.
emplace_back : constructs the object directly at the container's end, eliminating the copy/move step.
Underlying Data Structures of Map/Set Variants
Map : typically implemented with a red‑black tree (O(log n) operations).
Set : also a red‑black tree, storing only keys.
unordered_map : uses a hash table (average O(1) lookup).
unordered_set : hash table storing only keys.
Why Hash Tables Have O(1) Complexity
Hash tables compute a hash value for a key, which directly indexes an array slot, allowing constant‑time insertion, deletion, and lookup (ignoring collisions).
Handling Hash Collisions
When multiple keys map to the same slot, C++ STL resolves collisions using chaining (linked lists) inside each bucket.
Network Related
Differences Between TCP and UDP
TCP is connection‑oriented; UDP is connection‑less.
TCP provides reliable delivery with acknowledgments and retransmissions; UDP does not.
TCP is slower due to connection management; UDP is faster.
TCP transmits a byte stream; UDP transmits discrete datagrams.
TCP guarantees in‑order delivery; UDP does not.
TCP suits applications needing reliability (file transfer, email); UDP suits real‑time applications (audio/video, games).
How to Make UDP Reliable
Implement reliability at the application layer by adding sequence/ack mechanisms, send/receive buffers, and timeout‑based retransmission.
Sliding Window Implementation
The sender maintains a window of unacknowledged data; as ACKs arrive, the window slides forward, allowing new data to be sent. The receiver tracks received data, available space (RCV.WND), and the next expected byte (RCV.NXT).
Understanding TCP Stream Concept
TCP treats data as a continuous byte stream; a single user message may be split across multiple TCP segments, and the receiver must reassemble them based on length information or delimiters.
TCP Sticky‑Packet Problem and Solutions
Sticky packets occur when message boundaries are unknown. Common solutions:
Fixed‑length messages.
Special delimiter characters (e.g., HTTP CRLF).
Custom message format with a header indicating payload length.
struct {
u_int32_t message_length;
char message_data[];
} message;After reading the header, the receiver knows how many bytes to read for the complete message.
Operating System Related
Process Communication Methods
Linux provides several IPC mechanisms:
Anonymous pipes : unnamed, unidirectional, used between parent‑child processes.
Named pipes (FIFOs) : appear as special files, allow unrelated processes to communicate.
Message queues : store typed messages in the kernel, preserving message boundaries.
Shared memory : maps the same physical memory into multiple processes for fast data exchange.
Semaphores : synchronize access to shared resources (mutual exclusion and counting).
Signals : asynchronous notifications for events like SIGKILL or SIGSTOP.
Sockets : enable communication across hosts (TCP, UDP) or locally.
Difference Between Signals and Semaphores
Signals are asynchronous event notifications.
Semaphores are counting mechanisms for synchronization and mutual exclusion.
How Shared Memory Works
Shared memory maps a virtual address range of multiple processes to the same physical memory, allowing direct read/write without kernel copying.
Other Locks
Read‑write lock: multiple readers, single writer.
Spin lock: busy‑wait loop for short critical sections.
Condition variable: thread waits for a condition while holding a mutex.
Semaphore: counting lock for limiting concurrent access.
Program Memory Layout
The process address space (low to high) consists of:
Code segment (text).
Data segment (initialized globals).
BSS segment (uninitialized globals).
Heap (dynamic allocations, grows upward).
Memory‑mapped region (shared libraries, mmap).
Stack (local variables, grows downward, default ~8 MB).
Between code and data there is a reserved region to catch null‑pointer dereferences.
Where Static Variables Reside
Initialized static variables → data segment.
Uninitialized static variables → BSS segment.
Virtual‑to‑Physical Address Translation
The MMU translates virtual addresses using page tables (often multi‑level) and may employ a TLB cache for speed.
Algorithms
Write a quick‑sort implementation.
LeetCode problem: reverse nodes in k‑group linked list.
Interview Experience
The interview focused heavily on C++ fundamentals, system knowledge, and algorithm basics. Most questions were follow‑up style, probing deeper into each topic. The candidate felt prepared overall, though some algorithm details were a bit rusty.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
