This question evaluates stack-based simulation of single-threaded call traces, hierarchical path construction and frequency counting with deterministic tie-breaking by depth and first occurrence. It is commonly asked in the Coding & Algorithms domain to assess practical application of data structures and parsing logic for runtime logs rather than purely conceptual theory.
You are given a sequence of trace log lines for a single-threaded program. Each line is either a function entry or exit:
"-> funcName"
"<- funcName"
Maintain a call stack as you scan the logs from left to right.
Whenever you see an entry line, push the function onto the stack and form the full call path by joining the stack elements with "->" (including the top). For example, stack [main, handleEvents, handleClickEvent] corresponds to the path:
main->handleEvents->handleClickEvent
Count how many times each full call path occurs (only on entry events). Return the path string with the highest frequency.
If multiple paths have the same highest frequency:
If the input list is empty, return an empty string.
Example 1 Input:
["-> main",
"-> handleEvents",
"-> handleClickEvent",
"<- handleClickEvent",
"-> handleClickEvent",
"<- handleClickEvent",
"<- handleEvents",
"<- main"]
Output:
"main->handleEvents->handleClickEvent"
Example 2 Input:
["-> main",
"-> handleEvents",
"-> handleKeyEvent",
"<- handleKeyEvent",
"-> handleClickEvent",
"<- handleClickEvent",
"-> handleClickEvent",
"<- handleClickEvent",
"-> handleKeyEvent",
"<- handleKeyEvent",
"<- handleEvents",
"<- main"]
Output:
"main->handleEvents->handleClickEvent"
Example 3 Input:
[]
Output:
""
0 ≤ len(traces) ≤ 100,000
"-> "
or
"<- "
.
funcName
contains only letters, digits, or underscores.