Problem · Array

Most Frequent Call Path in Interleaved Logs

Learn this problem
MediumRoblox logoRobloxFULLTIMEONSITE INTERVIEW
See Roblox hiring insights

Problem statement

You are given a time-ordered array logs containing function-call events from multiple threads. Every event has one of these forms:

  • threadId|ENTER|functionName: push functionName onto that thread's call stack.
  • threadId|RETURN|functionName: functionName is the current top frame of that thread, so pop it.

Process the events in input order. Immediately after applying each event, if that event's thread has a non-empty stack, observe its complete active call path from the root frame to the current frame. Serialize a path by joining its function names with >.

Count equal path strings across all threads and all observations. Return the path with the greatest count. If several paths have the same greatest count, return the lexicographically smallest path.

Function

mostFrequentCallPath(logs: String[]) → String

Examples

Example 1

logs = ["t1|ENTER|main","t1|ENTER|load","t1|RETURN|load","t1|RETURN|main"]return = "main"

The observed non-empty paths are main, main>load, and main. Therefore main occurs twice.

Example 2

logs = ["t1|ENTER|api","t2|ENTER|worker","t1|ENTER|parse","t2|ENTER|parse","t1|RETURN|parse","t2|RETURN|parse","t1|RETURN|api","t2|RETURN|worker"]return = "api"

Paths api and worker each occur twice. The other two paths occur once, so the lexicographically smaller tied winner is api.

Example 3

logs = ["a|ENTER|root","b|ENTER|root","a|ENTER|x","b|ENTER|x","a|RETURN|x","b|RETURN|x","a|RETURN|root","b|RETURN|root"]return = "root"

Identical paths observed on different threads share one global frequency. Path root is observed four times, while root>x is observed twice.

Constraints

  • 2 <= logs.length <= 200000.
  • Every threadId and functionName is a non-empty case-sensitive string of letters, digits, or underscores.
  • Every return event matches the current top frame of its thread.
  • Every thread begins with an entry event and has a balanced call stack after the final event.
  • The total length of all event strings is at most 2 * 10^6.

More Roblox problems

drafts saved locally
public String mostFrequentCallPath(String[] logs) {
    // Write your code here.
}
logs["t1|ENTER|main","t1|ENTER|load","t1|RETURN|load","t1|RETURN|main"]
expected"main"
checking account