Trace Three Cooperative Constructors
Updated
Problem statement
Predict the print order for this explicitly defined Python-style cooperative constructor model. Class A has ID 0 and class B has ID 1; both directly inherit the implicit object base. A derived class C has ID 2 and lists A and B in the order supplied by baseOrder.
Instantiate C. Every named class has an initializer with exactly one call to super().__init__() and exactly one emission of its own ID. When before[id] == 1, emit that ID before calling super; when it is zero, emit it after super returns. The implicit object initializer emits nothing.
Cooperative super follows the instance’s method-resolution order. For these two independent bases, the chain is C, the first listed base, the second listed base, then object. Return the emitted IDs in order. Implementing a simulator in any supported language is sufficient; languages without this inheritance mechanism use the same stated trace rules.
The interview covers three classes, multiple inheritance, constructor print order and a discussion of super. This version fixes the hierarchy above with a Python-style convention.
Function
traceConstructors(baseOrder: int[], before: int[]) → int[]Examples
Example 1
baseOrder = [0,1]before = [1,1,1]return = [2,0,1]C emits 2 and calls A; A emits 0 and calls B; B emits 1 and calls object. All emissions occur on the way into the chain.
Example 2
baseOrder = [1,0]before = [0,1,0]return = [1,0,2]C calls B before emitting. B emits 1, calls A, and A emits 0 after object returns. Finally C emits 2 as the outer call returns.
Constraints
baseOrder.length == 2, andbaseOrderis either[0,1]or[1,0].before.length == 3and0 <= before[i] <= 1.- Exactly the three named classes print once each; the implicit object base prints nothing.
More JP Morgan Chase Onsite Interview questions
- LFU CacheONSITE INTERVIEW · Seen Nov 2026
- LRU CacheONSITE INTERVIEW · Seen Nov 2026
- Employees Above Average SalaryONSITE INTERVIEW · Seen Sep 2026
- Highest-Earning Employees By DepartmentONSITE INTERVIEW · Seen Sep 2026
- K Largest Integers in Descending OrderONSITE INTERVIEW · Seen Aug 2026
- Tile a Three-Row Board with DominoesONSITE INTERVIEW · Seen Jan 2026
- Two SumONSITE INTERVIEW · Seen Nov 2025
- Intersection of Two ListsONSITE INTERVIEW · Seen Jan 2025
- Maximum Subarray SumONSITE INTERVIEW · Seen Jan 2025
- Fibonacci NumberONSITE INTERVIEW · Seen Nov 2024
Similar questions at other companies
- Merge Sort Report with a Noisy ComparatorMercor · ONSITE INTERVIEWMercor interview questions
- Longest Consecutive Ones Across Distributed PartitionsMoloco · ONSITE INTERVIEW
- Last Person Left in a Counting CircleExpedia · ONSITE INTERVIEWExpedia interview questions
- Random Forest Classifier from ScratchMicrosoft · PHONE SCREENMicrosoft interview questions
- Serialize a Nested Dictionary as JSONGoogle · PHONE SCREENGoogle interview questions
- Ring Buffer OperationsAmazon · ONSITE INTERVIEW
- Count Distinct Underwriting PII ValuesAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Bounded Word Guess SolverAtlassian · ONSITE INTERVIEWAtlassian interview questions
- Insert After a Linked-List NodeAMD · ONSITE INTERVIEWAMD interview questions
- Document Edit EventsClickUp · ONSITE INTERVIEW
Interview reports that mention this question
JP Morgan Chase interview experiences
- JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)