Quick 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.

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

  1. A call is a leaf call exactly when the next event in the log is its own exit.
  2. 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.
  3. The tie-break is Python's list ordering: the first differing name decides, and a chain that is a prefix of another comes first.

Most Frequent Leaf Call Chain in an Execution Log

mediumRobloxSoftware Engineer

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

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

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.

Examples

Example 1
Input
([['call', 'main'], ['exit', 'main']],)
Output
['main']
Notes
Minimum valid log (2 events): a single top-level leaf call gives a one-name chain.
Example 2
Input
([['call', 'main'], ['call', 'load'], ['exit', 'load'], ['call', 'render'], ['call', 'draw'], ['exit', 'draw'], ['call', 'draw'], ['exit', 'draw'], ['exit', 'render'], ['exit', 'main']],)
Output
['main', 'render', 'draw']
Notes
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.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def most_frequent_call_chain(events: list[list[str]]) -> list[str]:
Sample call
most_frequent_call_chain([['call', 'main'], ['exit', 'main']])
Language
Case 1
Input
([['call', 'main'], ['exit', 'main']],)
Expected
['main']
Case 2
Input
([['call', 'main'], ['call', 'load'], ['exit', 'load'], ['call', 'render'], ['call', 'draw'], ['exit', 'draw'], ['call', 'draw'], ['exit', 'draw'], ['exit', 'render'], ['exit', 'main']],)
Expected
['main', 'render', 'draw']

Browse More Questions

Show the approach

Approach

Walk the log once with an explicit stack, but never copy the stack. Every distinct chain gets a node in a trie (prefix tree): the node of a chain is the child, labelled with the newly called name, of the node of the chain beneath it, and the root stands for the empty stack. Each stack frame stores its node, so a call costs one dictionary lookup, equal chains always reach the same node and different chains reach different nodes.

Leaf detection: a call is a leaf call exactly when the very next event is its own exit. If another call starts before that exit, the event right after the call is a call; and no exit can come between a call and its own exit unless some later call pushed the frame being popped. So a flag recording whether the previous event was a call identifies leaf calls: on an exit with the flag set, add one to the count of the top frame's node, whose chain is exactly the stack at the moment of that call. The flag is cleared after every exit, so a parent's exit that follows a child's exit adds nothing.

Tie-break: a pre-order traversal of the trie that visits children in increasing name order lists chains in exactly Python's list order. A node precedes all of its extensions (a prefix chain is smaller), and every chain under a smaller sibling name precedes every chain under a larger one (the first differing name decides). The first node met whose count equals the maximum is therefore the lexicographically smallest most frequent chain; its names are read back through parent links and reversed. The traversal uses an explicit stack because chains can be 1,000 deep.

Edge cases: the root never gets a count; the first exit of any valid log directly follows a call, so the maximum count is at least 1 and the answer is never empty; recursion just creates deeper nodes with repeated names; names are ASCII, so string comparison uses the same character-code order in Python, JavaScript, Java and C++. Counts are at most 10^4, so 32-bit integers suffice.

Time complexity:
O(n log n)
Space complexity:
O(n)