

























A software system logs its execution traces as a sequence of program events. Each event records either a function call or a function return.
Each entry in the input list traces is one of:
"-> function_name": Indicates that function_name has been called. It is added to the top of the current call chain.
"<- function_name": Indicates that function_name has returned. It is removed from the top of the call chain.
The Goal:
At any point, the "call chain" represents the list of active function calls, ordered from the first called (bottom) to the most recent (top).
Crucially: Every time a function is called (only for -> events), you must record what the current call chain looks like immediately after that call.
Find and return the call chain that occurs most frequently throughout the entire trace.
If the input traces is: ["-> A", "-> B", "<- B", "-> B", "<- B", "-> C"]
-> A: Call chain is ["A"]. Record it.
-> B: Call chain is ["A", "B"]. Record it.
<- B: (Return, do nothing but update stack).
-> B: Call chain is ["A", "B"]. Record it.
<- B: (Return, do nothing).
-> C: Call chain is ["A", "C"]. Record it.
Recorded Chains:
["A"]: 1 time
["A", "B"]: 2 times
["A", "C"]: 1 time
Result: ["A", "B"]
If multiple call chains have the same maximum frequency, the problem usually specifies returning the one that occurred first in the log, or the one that is lexicographically smallest. (In most Roblox variations of this, it's the one that reached that frequency first).
Input: traces (a list of strings).
Output: A list of strings representing the most frequent chain.
Constraints: You need to handle potentially deep stacks and many logs, so using a dictionary (hash map) to store the counts of the chains (converted to tuples) is the most efficient approach.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。