Find most frequent call stack from logs
Company: Roblox
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates a candidate's ability to simulate and maintain a runtime call stack from event logs, parse entry and exit records, and aggregate snapshot frequencies using appropriate data-structure and string-manipulation skills.
Constraints
- 0 <= len(logs) <= 20000
- Each function name has length between 1 and 20 and contains only letters, digits, or underscores
- The logs form a valid call/return sequence for a single-threaded program
- The maximum call-stack depth does not exceed 200
Examples
Input: ['->A','->B','->C','<-C','->C','<-C','<-B','<-A']
Expected Output: ('A->B->C', 2)
Explanation: The entry snapshots are 'A', 'A->B', 'A->B->C', and again 'A->B->C'. So 'A->B->C' appears 2 times.
Input: ['->A','<-A','->A','<-A','->B','<-B']
Expected Output: ('A', 2)
Explanation: The snapshots are 'A', 'A', and 'B'. The most frequent snapshot is 'A' with count 2.
Hints
- Use a stack to simulate the current call stack, and only count snapshots after '->Name' events.
- If rebuilding the whole snapshot every time feels wasteful, represent each stack state by its parent state plus the new function name and reuse states that appear again.
Community answers
Answer by tonychungyay