PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick 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.

  • Medium
  • Roblox
  • Coding & Algorithms
  • Software Engineer

Find most frequent call stack from logs

Company: Roblox

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given an array of log entries for a single-threaded program's function calls, each entry is either '->Name' (function entry) or '<-Name' (function exit). The call stack updates accordingly. Consider the call-stack snapshot immediately after each '->Name' (root at the left, top at the right) and represent it as a string like 'A->B->C'. Return the snapshot that appears most frequently across the entire log and the number of times it occurs. Example: ['->A','->B','->C','<-C','->C','<-C','<-B','<-A'] should return 'A->B->C' with count 2. Explain your approach, analyze time/space complexity, and provide working code.

Quick Answer: 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.

You are given a list of log entries for a single-threaded program. Each entry is either '->Name' (a function is entered) or '<-Name' (the current function exits). The logs are valid, so every exit matches the most recent unmatched entry. After every '->Name' event, record the current call-stack snapshot from root to top, joined by '->'. For example, if the current stack is [A, B, C], the snapshot string is 'A->B->C'. Return the snapshot that appears most frequently across the entire log and the number of times it appears. If multiple snapshots have the same highest frequency, return the lexicographically smallest snapshot. If there are no entry events at all, return ('', 0). A function may call itself recursively, so the same name can appear multiple times in one snapshot.

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.

Input: ['->A','->A','<-A','->A','<-A','<-A']

Expected Output: ('A->A', 2)

Explanation: This is a recursive call pattern. The snapshots are 'A', 'A->A', and 'A->A', so 'A->A' occurs 2 times.

Input: ['->B','<-B','->A','<-A','->C','<-C']

Expected Output: ('A', 1)

Explanation: All snapshots appear once: 'B', 'A', and 'C'. By the tie rule, return the lexicographically smallest one, which is 'A'.

Input: []

Expected Output: ('', 0)

Explanation: There are no entry events, so there are no snapshots to count.

Input: ['->Main','->Load','<-Load','->Load','<-Load','->Run','<-Run','<-Main']

Expected Output: ('Main->Load', 2)

Explanation: The snapshots are 'Main', 'Main->Load', 'Main->Load', and 'Main->Run'. 'Main->Load' appears 2 times.

Solution

def solution(logs):
    # Node 0 represents the empty stack.
    parents = [-1]
    snapshots = [""]
    children = [{}]
    counts = [0]

    current = 0
    best_snapshot = ""
    best_count = 0

    for entry in logs:
        name = entry[2:]

        if entry.startswith("->"):
            next_node = children[current].get(name)

            if next_node is None:
                next_node = len(parents)
                children[current][name] = next_node
                parents.append(current)
                if current == 0:
                    snapshots.append(name)
                else:
                    snapshots.append(snapshots[current] + "->" + name)
                children.append({})
                counts.append(0)

            current = next_node
            counts[current] += 1
            snap = snapshots[current]

            if counts[current] > best_count or (counts[current] == best_count and snap < best_snapshot):
                best_count = counts[current]
                best_snapshot = snap
        else:
            # Logs are guaranteed to be valid.
            if current != 0:
                current = parents[current]

    return (best_snapshot, best_count)

Time complexity: O(n + S), where n is the number of log entries and S is the total length of distinct snapshot strings created. Space complexity: O(U + S), where U is the number of distinct stack states and S is the total length of distinct snapshot strings stored.

Hints

  1. Use a stack to simulate the current call stack, and only count snapshots after '->Name' events.
  2. 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.
Last updated: May 1, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Windows Containing a Target - Roblox (medium)
  • Implement Sliding-Window Rate Limiter - Roblox (medium)
  • Find target-heavy sliding windows - Roblox (medium)
  • Find most frequent call path in logs - Roblox (medium)
  • Track Highest-Earning Experience - Roblox (medium)