← sunrise Interview Insights

sunrise·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Interviewed for a Software Engineer role at Sunrise, and the technical questions leaned pretty heavily into C++ internals. Not the most grueling round but you really need to know your object model cold.

Questions Asked (3)

Q1

Can a constructor be virtual in C++, and why or why not?

Technical Trade-offsSystem Design
Author's notes

I knew the answer but fumbled the explanation a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by directly answering 'No, a constructor cannot be virtual in C++.' Then explain the fundamental reason: virtual functions rely on the vtable, which is set up after the constructor runs, so virtual dispatch during construction is impossible. Finally, clarify what problem people might be trying to solve (e.g., polymorphic creation) and present alternatives like factory methods or the Virtual Constructor idiom.

Pro tip: Mention that while constructors can't be virtual, destructors should be virtual in polymorphic base classes—this shows you understand the broader object lifecycle and common pitfalls.

1. Direct Answer

State clearly that constructors cannot be virtual in C++. Avoid hedging; this is a definitive language rule.

2. Explain Why

Describe the object construction order: base class constructor runs before derived class constructor, and the vtable pointer is set up only after the base constructor completes. Thus, virtual dispatch cannot work during construction.

3. Address the Underlying Need

Acknowledge that the question often arises from a need to create objects polymorphically. Explain that this is a design problem, not a language feature.

4. Present Alternatives

Discuss solutions like factory methods, the Virtual Constructor idiom (clone() method), or using a separate initialization function.

5. Contrast with Destructors

Highlight that destructors can and should be virtual in polymorphic base classes to ensure proper cleanup, reinforcing your understanding of object lifetime.

Key Points to Mention

  • Virtual functions require a vtable, which is set up after the constructor runs.
  • During base class construction, the object's dynamic type is the base class, so virtual calls would resolve to base versions.
  • The Virtual Constructor idiom: a virtual clone() or create() method to achieve polymorphic creation.
  • Factory design pattern as a common alternative for creating objects without specifying exact class.
  • Destructors should be virtual in polymorphic base classes to avoid undefined behavior.
  • C++ standard explicitly prohibits virtual constructors (e.g., [class.virtual] in the standard).

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

What is the 'virtual constructor' idiom in C++ and how does it work?

System DesignTechnical Trade-offs
Author's notes

This is the clone() pattern where you put a virtual method on the base class that returns a new heap-allocated copy of whatever the dynamic type actually is.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying that C++ has no true virtual constructor, then explain the idiom as a factory method that uses a virtual clone/create function to construct objects of derived types. Describe how it works with a base class declaring a virtual function returning a pointer to a new object, and derived classes overriding it to return their own type. Finally, discuss its use in cloning and polymorphic object creation, and mention trade-offs like manual memory management and the need for a virtual destructor.

Pro tip: Emphasize that the idiom is often implemented via a virtual clone() method for polymorphic copying, and mention that modern C++ (C++11 and later) offers safer alternatives like smart pointers and std::make_shared to manage ownership, reducing the risk of memory leaks.

1. Clarify the terminology

Explain that C++ does not support virtual constructors directly, but the idiom simulates them using a virtual function that creates an object.

2. Describe the classic implementation

Outline a base class with a virtual function (e.g., clone() or create()) that returns a pointer to a new object, and derived classes override it to return their own type.

3. Explain how it enables polymorphic creation

Show how a client can call the virtual function through a base pointer to create an object of the derived type without knowing the concrete type.

4. Discuss use cases and trade-offs

Mention scenarios like cloning objects in a container, and trade-offs such as manual memory management, need for virtual destructor, and potential for slicing if not used carefully.

5. Mention modern alternatives

Briefly note that smart pointers and factory patterns can provide safer, more modern solutions, and that the idiom is less common in modern C++.

Key Points to Mention

  • C++ lacks direct virtual constructor support; the idiom simulates it.
  • Implementation typically uses a virtual clone() or create() method returning a pointer to a new object.
  • Derived classes override the virtual function to return their own type.
  • Enables polymorphic object creation and copying without knowing the concrete type.
  • Requires a virtual destructor in the base class to avoid undefined behavior.
  • Trade-offs include manual memory management and potential for memory leaks; modern C++ prefers smart pointers and factory functions.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

Why is a virtual destructor necessary for polymorphic base classes in C++?

Technical Trade-offsSystem Design
Author's notes

Easier than the constructor question.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining the core issue: deleting a derived object through a base pointer without a virtual destructor leads to undefined behavior because only the base destructor is called. Then, describe how a virtual destructor ensures the derived destructor runs first, followed by the base destructor, preventing resource leaks. Finally, mention the trade-offs, such as vtable overhead, and when to use it.

Pro tip: Mention that even though the base destructor is called, the derived destructor is not, which can cause resource leaks—this shows you understand the practical consequences beyond just the language rule. Also, note that if a class is not intended to be used polymorphically, a virtual destructor is unnecessary and adds overhead.

1. Define the problem

Explain that when a derived object is deleted via a base class pointer, only the base destructor is invoked if it's not virtual, leading to undefined behavior and resource leaks.

2. Explain the mechanism

Describe how a virtual destructor ensures the derived destructor is called first, then the base destructor, via dynamic dispatch.

3. Discuss consequences

Highlight that without a virtual destructor, derived class resources (memory, file handles, etc.) are not released, causing leaks and potential crashes.

4. Address trade-offs

Mention that adding a virtual destructor introduces a vtable pointer, increasing object size and preventing certain optimizations, so it should only be used for polymorphic base classes.

5. Provide best practices

State that any class with virtual functions should have a virtual destructor, and if a class is not meant to be deleted polymorphically, consider making the destructor protected and non-virtual.

Key Points to Mention

  • Undefined behavior when deleting derived object through base pointer without virtual destructor
  • Only base destructor called, leading to resource leaks in derived class
  • Virtual destructor ensures derived destructor runs first, then base destructor
  • Vtable overhead and object size increase
  • Rule: virtual destructor if class has any virtual functions
  • Alternative: protected non-virtual destructor for non-polymorphic base classes

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.