Most Frequent Call Chain From a Log of Function Calls and Exits
Company: Roblox
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
A program's execution is recorded as a log of function calls and exits, in the order they happened. Find the call chain that occurs most often in the log.
The call stack starts empty. A **call chain** is the list of functions on the call stack at the moment a function is called, from the outermost function to the function just called. Only *leaf calls* produce chain occurrences: a call is a leaf call when no other call starts between it and its matching exit. Each leaf call adds one occurrence of the chain that ends with it.
### Function Signature
```python
def most_frequent_call_chain(events: list[list[str]]) -> list[str]:
```
### Rules
- Each event is a pair `[action, name]`: `action` is `"call"` or `"exit"`, and `name` is a function name.
- A `"call"` pushes `name` onto the call stack. An `"exit"` pops the top of the stack, and its `name` always equals the function being popped.
- The log may contain several top-level calls one after another, and a function may call itself directly or indirectly, so a name can appear more than once in a chain.
- A call that starts another call before it exits is not a leaf call and adds no occurrence of its own chain.
- Two occurrences belong to the same chain only if they list the same names in the same order.
- If several chains share the highest count, return the lexicographically smallest one: compare the chains name by name as strings, and if one chain is a prefix of the other, the shorter chain is smaller. This is the ordering Python uses for lists of strings.
- Return the chain as a list of names, outermost first.
### Constraints
- `2 <= len(events) <= 2 * 10^4`
- Every name has 1 to 20 characters, each a lowercase English letter, a digit or an underscore.
- The log is well formed: every exit matches the function on top of the stack, and the stack is empty after the last event. So `len(events)` is even and the log contains at least one call.
- The call stack never holds more than 1,000 functions at once.
### Examples
**Example 1**
```text
Input: events = [["call", "main"], ["call", "load"], ["exit", "load"],
["call", "render"], ["call", "draw"], ["exit", "draw"],
["call", "draw"], ["exit", "draw"], ["exit", "render"],
["exit", "main"]]
Output: ["main", "render", "draw"]
```
The leaf calls are `load` once and `draw` twice, so `["main", "load"]` occurs once and `["main", "render", "draw"]` occurs twice. `main` and `render` start other calls, so they are not leaf calls.
**Example 2**
```text
Input: events = [["call", "a"], ["call", "c"], ["exit", "c"], ["exit", "a"],
["call", "b"], ["exit", "b"],
["call", "a"], ["call", "c"], ["exit", "c"], ["exit", "a"],
["call", "b"], ["exit", "b"]]
Output: ["a", "c"]
```
`["a", "c"]` and `["b"]` each occur twice. Their first names compare as `"a" < "b"`, so `["a", "c"]` is returned.
**Example 3**
```text
Input: events = [["call", "f"], ["call", "f"], ["exit", "f"], ["exit", "f"]]
Output: ["f", "f"]
```
The inner call of `f` is the only leaf call, and at that moment the stack holds `f` twice.
Overview: A coding problem that gives an ordered log of function call and exit events and asks for the call chain, from the outermost function down to a leaf call, that occurs most often, with ties broken lexicographically. It tests stack reasoning over nested and recursive calls, counting whole sequences, and precise tie handling.
Read the full Roblox Software Engineer interview experience this question came from
A program's execution is recorded as a log of function calls and exits, in the order they happened. Find the call chain that occurs most often in the log.
The call stack starts empty. A **call chain** is the list of functions on the call stack at the moment a function is called, from the outermost function to the function just called. Only *leaf calls* produce chain occurrences: a call is a leaf call when no other call starts between it and its matching exit. Each leaf call adds one occurrence of the chain that ends with it.
Implement `most_frequent_call_chain(events)`, which returns the chain with the highest number of occurrences.
### Rules
- Each event is a pair `[action, name]`: `action` is `"call"` or `"exit"`, and `name` is a function name.
- A `"call"` pushes `name` onto the call stack. An `"exit"` pops the top of the stack, and its `name` always equals the function being popped.
- The log may contain several top-level calls one after another, and a function may call itself directly or indirectly, so a name can appear more than once in a chain.
- A call that starts another call before it exits is not a leaf call and adds no occurrence of its own chain.
- Two occurrences belong to the same chain only if they list the same names in the same order.
- If several chains share the highest count, return the lexicographically smallest one: compare the chains name by name as strings, and if one chain is a prefix of the other, the shorter chain is smaller. This is the ordering Python uses for lists of strings (names compare character by character by character code, so digits come before `_`, which comes before lowercase letters).
- Return the chain as a list of names, outermost first.
### Constraints
- `2 <= len(events) <= 2 * 10^4`
- Every name has 1 to 20 characters, each a lowercase English letter, a digit or an underscore.
- The log is well formed: every exit matches the function on top of the stack, and the stack is empty after the last event. So `len(events)` is even and the log contains at least one call.
- The call stack never holds more than 1,000 functions at once.
### Example 1
```text
Input: events = [["call", "main"], ["call", "load"], ["exit", "load"],
["call", "render"], ["call", "draw"], ["exit", "draw"],
["call", "draw"], ["exit", "draw"], ["exit", "render"],
["exit", "main"]]
Output: ["main", "render", "draw"]
```
The leaf calls are `load` once and `draw` twice, so `["main", "load"]` occurs once and `["main", "render", "draw"]` occurs twice. `main` and `render` start other calls, so they are not leaf calls.
### Example 2
```text
Input: events = [["call", "a"], ["call", "c"], ["exit", "c"], ["exit", "a"],
["call", "b"], ["exit", "b"],
["call", "a"], ["call", "c"], ["exit", "c"], ["exit", "a"],
["call", "b"], ["exit", "b"]]
Output: ["a", "c"]
```
`["a", "c"]` and `["b"]` each occur twice. Their first names compare as `"a" < "b"`, so `["a", "c"]` is returned.
Constraints
- 2 <= len(events) <= 2 * 10^4
- Each event is a pair [action, name] where action is "call" or "exit".
- Every name has 1 to 20 characters, each a lowercase English letter, a digit or an underscore.
- The log is well formed: every exit matches the function on top of the stack, and the stack is empty after the last event. So len(events) is even and the log contains at least one call.
- The call stack never holds more than 1,000 functions at once.
Examples
Input: ([['call', 'main'], ['exit', 'main']],)
Expected Output: ['main']
Explanation: Minimum valid log (2 events): a single top-level leaf call gives a one-name chain.
Input: ([['call', 'main'], ['call', 'load'], ['exit', 'load'], ['call', 'render'], ['call', 'draw'], ['exit', 'draw'], ['call', 'draw'], ['exit', 'draw'], ['exit', 'render'], ['exit', 'main']],)
Expected Output: ['main', 'render', 'draw']
Explanation: Source example 1 (10 events): load is a leaf once and draw twice under render; main and render start other calls, so they add nothing.
Hints
- A call is a leaf call exactly when the next event in the log is its own exit.
- Copying the whole stack at every leaf call can cost up to (stack depth) x (number of leaf calls); chains that share a prefix can share the work of identifying them.
- The tie-break is Python's list ordering: the first differing name decides, and a chain that is a prefix of another comes first.