Problem · Stack

Most Frequent Call Path From Function Trace Logs

Learn this problem
MediumRoblox logoRobloxFULLTIMEPHONE SCREEN
See Roblox hiring insights

Problem statement

You are given single-threaded function trace logs. Each line records either entering a function or returning from one. Scan the logs from left to right while maintaining the active call stack.

Whenever a function is entered, count the full active call path from the root to the current function. Join function names with ->.

Return the path with the greatest frequency. If frequencies tie, return the deeper path. If frequency and depth both tie, return the path whose first appearance in the trace occurs earlier.

Each line is "-> name" for entry or "<- name" for return. This executable version also accepts no space after the arrow. The trace is well formed, so every return matches the active stack's top.

Function

mostFrequentCallPath(traces: String[]) → String

Examples

Example 1

traces = ["-> main", "-> handleEvents", "-> handleClickEvent", "<- handleClickEvent", "-> handleClickEvent", "<- handleClickEvent", "<- handleEvents", "<- main"]return = "main->handleEvents->handleClickEvent"

The paths main and main->handleEvents are each counted once. The path main->handleEvents->handleClickEvent is counted twice, so it is returned.

Example 2

traces = ["-> main", "-> handleEvents", "-> handleKeyEvent", "<- handleKeyEvent", "-> handleClickEvent", "<- handleClickEvent", "-> handleClickEvent", "<- handleClickEvent", "-> handleKeyEvent", "<- handleKeyEvent", "<- handleEvents", "<- main"]return = "main->handleEvents->handleKeyEvent"

The two leaf paths each occur twice at the same depth. The handleKeyEvent path appeared first, so it wins the final tie-break.

Example 3

traces = ["-> A", "-> B", "<- B", "<- A"]return = "A->B"

A and A->B both occur once. A->B is deeper, so it wins the tie.

Example 4

traces = []return = ""

There are no entered functions, so the answer is the empty string.

Example 5

traces = ["->main", "->worker", "<-worker", "<-main"]return = "main->worker"

The parser should accept trace lines without spaces after the arrow. The deeper path wins because both paths occur once.

Constraints

  • 0 <= traces.length <= 100000
  • Each line starts with -> or <-.
  • Function names contain letters, digits, or underscores.
  • The trace is well formed and balanced.
  • If traces is empty, return the empty string.

More Roblox problems

drafts saved locally
public String mostFrequentCallPath(String[] traces) {
  // write your code here
}
traces["-> main", "-> handleEvents", "-> handleClickEvent", "<- handleClickEvent", "-> handleClickEvent", "<- handleClickEvent", "<- handleEvents", "<- main"]
expected"main->handleEvents->handleClickEvent"
checking account