This video features a mock C++ interview for a quantitative developer position at a hedge fund. The interviewer, Coding Jesus, asks the candidate a series of challenging C++ questions, covering topics like lambdas, virtual functions, inheritance, null pointers, const object movement, and C++20 features. The candidate provides detailed and insightful answers, demonstrating a strong understanding of C++ concepts.
nullptr is preferred over 0 or NULL in C++ to avoid ambiguity in function overloading and to explicitly signify a null pointer.const object is not possible; the compiler will typically fall back to a copy operation, which can lead to silent failures if copy constructors are deleted.<=>), coroutines, and ranges are discussed, with a note on the current practicality and future potential of ranges.Here's a summary of the questions and answers from the interview, in order:
Question: How does a Lambda look like in C++? Answer: It has square brackets for the capture clause, curly braces for the function body, an arrow operator for the return type (optional), parameters within the curly braces, and a semicolon.
Question: Of the capture clause, argument clause, and body, which can be ignored in a Lambda? Answer: The return type can be skipped. The parameter list and capture clause can also potentially be skipped, but the function body cannot.
Question: A class Fu has no data members but one non-virtual method that takes nothing and returns nothing. How big is this object?
Answer: The size is likely one byte. It cannot be zero because that would violate the rule that distinct objects must have distinct addresses.
Question: What if that method in the previous question was virtual? Answer: The object would need a virtual table pointer (vptr), making it larger. On a 64-bit system, it would likely be 8 bytes, and on a 32-bit system, 4 bytes.
Question: What cost is incurred by adding a virtual method beyond the size increase? Answer: Dynamic dispatch. Calls made through pointers or references to virtual methods require an extra indirection through the vptr to the vtable to find the correct function, which can lead to more cache misses and is less predictable at compile time.
Question: What is virtual inheritance? Answer: It's used to bypass the diamond problem, ensuring that when multiple inheritance paths lead to the same base class, only one instance of that base class is included in the final derived object.
Question: Why should nullptr be used instead of 0 or NULL?
Answer: NULL was often a type definition for 0. Using 0 or NULL with overloaded functions (e.g., one taking an int, another a pointer) could lead to ambiguity. nullptr is a distinct type (std::nullptr_t) that can only bind to pointer types, making the intent explicit and avoiding overload resolution ambiguity.
Question: You have a const object fu of type A. You then declare bar of type A and initialize it with std::move(fu). What happens to fu if fu has resources?
Answer: You cannot move a const object because moving implies modifying the source object to transfer its resources. Attempting to do so will result in a compilation error because the const qualifier prevents the necessary static_cast to an rvalue reference.
Question: What if the class had its copy constructor deleted?
Answer: If the copy constructor was deleted, and moving a const object defaults to a copy, then attempting to move a const object whose copy constructor is deleted would result in a compilation error.
Question: What's your favorite C++20 feature?
Answer: The candidate likes the spaceship operator (<=>), coroutines, and ranges. They expressed some reservations about the current practicality of ranges due to the lack of certain features (like a pipeline to a container) in C++20, which might be addressed in C++23.
Question: Tip for interviews: If asked about your favorite features, and you say "what else," what's the advice? Answer: Don't say "what else." Let the interviewer prompt you for more by asking, "Is there anything else?" This prevents you from opening the door to further questions when they might have been ready to move on.
Question: Class A has a virtual method (not pure virtual) and its constructor calls this method. Class B inherits from A, overrides the method, and its constructor also calls this method. What gets printed when an instance of B is constructed? (Assume A's method prints 1, B's prints 2).
Answer: It prints '1'. During the construction of B, A's constructor runs first. At this point, the object is still considered of type A, so the virtual call resolves to A's method.
Question: What if A's method was a pure virtual method with no implementation, and B overrides it to print '2'?
Answer: This scenario leads to undefined behavior. You cannot instantiate a class with pure virtual functions, and attempting to call a pure virtual function that has no implementation (even if called via a derived class's constructor before the derived class is fully constructed) results in undefined behavior.
Question: Re-clarification on the pure virtual method scenario: If the compiler treats it like a static method when not touching members, could it work? Answer: The interviewer clarifies that it's generally undefined behavior to call a pure virtual method without an implementation, even if the derived class has one and it's called during construction. The compiler might not treat it as static in this context.