← TikTok Interview Insights

TikTok·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
May 2026

Summary

TikTok ML Engineer interview that went pretty deep into C++ internals, which I wasn't fully expecting for this role. The virtual function stuff caught me mid-stride and I had to think harder than I'd like to admit.

Questions Asked (3)

Q1

In C++, what is the difference between a virtual function and a pure virtual function? Walk through the syntax, explain how the vtable enables dynamic dispatch, and describe how overriding works in derived classes.

Technical Trade-offsSystem Design
Author's notes

I knew the basics but fumbled explaining vtable mechanics under pressure.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining virtual and pure virtual functions with syntax examples, then explain how the vtable enables dynamic dispatch, and finally describe overriding in derived classes. Emphasize the practical implications for polymorphism and design patterns relevant to ML engineering.

Pro tip: Connect the concept to real-world ML systems, such as using virtual functions in plugin architectures for model serving or strategy patterns for interchangeable algorithms, to demonstrate applied knowledge.

1. Define virtual and pure virtual functions

Explain that a virtual function has an implementation and can be overridden, while a pure virtual function has no implementation and makes the class abstract. Provide syntax examples: `virtual void foo();` and `virtual void bar() = 0;`.

2. Explain the vtable and dynamic dispatch

Describe how the compiler creates a vtable for each class with virtual functions, containing pointers to the most-derived implementations. Explain that objects have a hidden vptr pointing to the vtable, enabling runtime resolution of calls.

3. Describe overriding in derived classes

Detail how a derived class can override a virtual function by providing its own implementation with the same signature. Mention that pure virtual functions must be overridden unless the derived class is also abstract.

4. Highlight practical implications

Discuss how this enables polymorphism, allowing base class pointers to invoke derived class methods. Relate to design patterns like Strategy or Template Method, common in ML pipelines.

Key Points to Mention

  • Virtual functions have implementations; pure virtual functions do not and make the class abstract.
  • Syntax: `virtual` keyword for virtual functions; `= 0` for pure virtual.
  • Vtable is a static array of function pointers per class; vptr is a hidden member in objects.
  • Dynamic dispatch resolves function calls at runtime based on the object's actual type.
  • Overriding requires exact signature match; use `override` keyword for safety.
  • Pure virtual functions enforce interface contracts, useful for plugin architectures in ML systems.

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

Q2

When does a class become abstract in C++, and can a pure virtual function still have a body or definition?

Technical Trade-offs
Author's notes

The second part tripped me up.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining when a class becomes abstract in C++: it must have at least one pure virtual function. Then clarify that pure virtual functions can indeed have a body, but the class remains abstract and cannot be instantiated. Use a concrete example to illustrate the syntax and behavior, and mention the implications for design patterns like interfaces.

Pro tip: Emphasize that a pure virtual function with a body is useful for providing default behavior that derived classes can explicitly call, but it does not make the class concrete. This shows deep understanding of C++ semantics and practical design considerations.

1. Define abstract class

State that a class becomes abstract when it contains at least one pure virtual function (declared with = 0). Such a class cannot be instantiated directly.

2. Explain pure virtual function

Describe that a pure virtual function is declared by assigning 0 in its declaration, and it makes the class abstract. Derived classes must override it to become concrete.

3. Address the body question

Clarify that a pure virtual function can have a separate definition (body) outside the class. This does not change the abstract nature of the class.

4. Provide example

Give a short code snippet showing a pure virtual function with a body, and how a derived class can call it explicitly using the base class scope resolution operator.

5. Discuss use cases

Mention that this technique is useful for providing default implementations while forcing derived classes to override, common in interface design and template method pattern.

Key Points to Mention

  • A class is abstract if it has at least one pure virtual function.
  • Pure virtual functions are declared with '= 0' in the class declaration.
  • A pure virtual function can have a definition (body) outside the class.
  • The class remains abstract even if the pure virtual function has a body.
  • Derived classes must override the pure virtual function to be instantiable.
  • Derived classes can call the base class's pure virtual function implementation explicitly.

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

Q3

Should constructors or destructors be virtual in C++? What are the implications for resource management, and when would you use each in interface design?

Technical Trade-offsSystem Design
Author's notes

Destructors yes, constructors no, and I knew that much.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying that constructors cannot be virtual, while destructors should be virtual when deleting derived objects through base pointers. Then discuss the resource management implications, such as proper cleanup and avoiding leaks, and relate to interface design by recommending virtual destructors in polymorphic base classes and non-virtual constructors with factory patterns.

Pro tip: Mention that a virtual destructor can introduce a vtable pointer, increasing object size, which matters in performance-critical ML systems; use it judiciously. Also, note that making constructors virtual is impossible, but the Virtual Constructor idiom (clone method) can achieve polymorphic copying.

1. Clarify constructor vs. destructor virtuality

State that constructors cannot be virtual because virtual dispatch requires an existing object, while destructors can and should be virtual in polymorphic base classes.

2. Explain resource management implications

Discuss how a virtual destructor ensures derived class destructors are called when deleting via base pointer, preventing resource leaks. Mention that non-virtual destructors cause undefined behavior in such cases.

3. Discuss interface design guidelines

Recommend virtual destructors for base classes intended for polymorphic deletion. For constructors, suggest factory methods or the Virtual Constructor idiom (clone) for polymorphic creation.

4. Address performance and trade-offs

Note that virtual destructors add a vtable pointer, increasing object size and potentially impacting cache performance, which is relevant in ML systems. Weigh this against safety.

5. Relate to ML engineering context

Give examples like model interfaces where derived models (e.g., different architectures) are managed via base pointers, requiring virtual destructors for safe cleanup.

Key Points to Mention

  • Constructors cannot be virtual; destructors should be virtual in polymorphic base classes.
  • Virtual destructors ensure proper cleanup of derived resources when deleting through base pointers.
  • Non-virtual destructors lead to undefined behavior and resource leaks in polymorphic deletion.
  • Virtual destructors introduce a vtable pointer, increasing object size and affecting performance.
  • Use the Virtual Constructor idiom (clone method) for polymorphic object creation.
  • In ML systems, virtual destructors are crucial for managing heterogeneous model objects safely.

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