Your team is debugging a program’s performance. A tracing tool records all function calls and returns, producing a sequence of events. Your task is to analyze these raw traces and determine which call stack occurs most frequently.
Identifying the most frequent call stack will provide insights into where the main slowdown may be occurring.
Input:
- An array of strings, where each string represents a traced function call or return
- Function calls have the prefix
->(e.g.-> main) - Function returns have the prefix
<-(e.g.<- main)
Output:
- Return the call stack as a string with function names separated by
->that occurs most frequently - If there’s a tie in frequency, return the call stack that achieves the top frequency first among those tied
Call Stack Definition:
- A call stack is the sequence of active function calls at any point in time
- For example, if
main()callshandleEvents(), which callshandleClickEvent(), the call stack ismain->handleEvents->handleClickEvent - Returning from a function to a previous call stack does not create a new call stack for the purpose of counting occurrences
Example:
traces = [
"-> main",
"-> handleEvents",
"-> handleClickEvent",
"<- handleClickEvent",
"-> handleClickEvent",
"-> handleClickEvent",
"<- handleClickEvent",
"<- handleEvents",
"<- main"
]
In the above example, the call stack main->handleEvents->handleClickEvent occurs twice, which is the most frequent.
This problem asks you to process a sequence of function call and return events, maintain the current call stack, and count how often each active stack appears. A stack data structure is the natural fit: push on calls, pop on returns, then serialize the current stack as a key such as <code>main->handleEvents->handleClickEvent</code>. The answer is the most frequent stack, with ties broken by the one that first reaches the highest count.