← JP Morgan Interview Insights

JP Morgan·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

JP Morgan quant engineer interview with a C++ code reading question focused on virtual functions and inheritance. Pretty niche stuff if you haven't touched vtables in a while.

Questions Asked (1)

Q1

You're given a C++ code snippet that uses virtual functions and inheritance. Walk through what the output will be and explain why, covering dynamic dispatch, vtable mechanics, and any gotchas around virtual destructors or object slicing.

Technical Trade-offsAlgorithms & Data Structures
Author's notes

This one stung a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by tracing the code's execution path, identifying which functions are virtual and how objects are constructed/destructed. Then explain the output by mapping each call to the appropriate vtable entry, highlighting dynamic dispatch and any pitfalls like slicing or non-virtual destructors. Finally, discuss the implications and trade-offs in a real-world context.

Pro tip: Mention that in financial systems like those at JP Morgan, virtual destructors are critical to avoid resource leaks in polymorphic hierarchies, and object slicing can silently corrupt data—demonstrating awareness of production-grade C++.

1. Identify class hierarchy and virtual functions

List all classes, their inheritance relationships, and which functions are declared virtual. Note any virtual destructors.

2. Trace object construction and destruction order

Determine the order of constructor and destructor calls for each object, considering base and derived classes.

3. Map function calls to vtable entries

For each call, determine whether it's resolved at compile-time (static) or runtime (dynamic) based on the static type and virtualness.

4. Analyze output and explain gotchas

Explain the printed output, highlighting any slicing, non-virtual destructor issues, or pure virtual calls.

5. Discuss implications and best practices

Summarize why the observed behavior matters and how to avoid pitfalls in production code.

Key Points to Mention

  • Dynamic dispatch via vtable and vptr, and how it enables runtime polymorphism.
  • Virtual destructors: why they are necessary for proper cleanup in polymorphic deletion.
  • Object slicing: what it is, when it occurs (e.g., passing by value), and how to prevent it.
  • Constructor/destructor call order and virtual calls during construction/destruction.
  • Pure virtual functions and abstract classes: implications for instantiation and calls.
  • Performance and memory overhead of virtual functions and vtables.

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