KatInterview

Aggregate Executed Call Sequences across Threads

Data Structure DesignAuto-graded · 14 test cases
MediumAuto-gradedData Structure Designdesign-dsarrayshash-table

Last updated

Simulate an execution stream aggregator tracking stack call frequency across threads with custom tie-breaking rules.

You are tasked with building an in-memory aggregation module for a call-stack sampling profiler. As worker threads run, the profiler periodically captures active call stacks, represented as ordered lists of frame strings from the entry frame down to the deepest active function call.

You need to process a sequence of commands given by operations and arguments:

  1. "record_sample" with args = [thread_id, stack]:

    • thread_id (string): identifier of the recorded thread.
    • stack (list of strings): execution path ordered from root to leaf.
    • Action: Log this sample into the profiler data structures.
    • Output: None (represented as null).
  2. "get_top_overall" with args = []:

    • Output: The most frequently recorded call stack across all threads along with its total count as [stack_list, count].
    • If no samples have been recorded yet, return None.
  3. "get_top_per_thread" with args = []:

    • Output: A dictionary mapping each observed thread_id to its individual most frequent call stack and sample count as {thread_id: [stack_list, count]}.
    • If no samples have been recorded yet, return an empty dictionary {}.

Tie-Breaking Rule

If two or more call stacks have the same highest frequency (globally or within a specific thread), choose the stack that is lexicographically smaller. Stacks are compared frame by frame from root to leaf using standard ASCII string ordering:

  • ["a", "b"] comes before ["a", "c"].
  • ["a"] comes before ["a", "b"].
  • An empty call stack [] comes before any non-empty stack.

Constraints

  • $1 \le N \le 100{,}000$ operations in total.
  • $1 \le \text{len}(thread_id) \le 32$.
  • $0 \le \text{len}(stack) \le 50$. An empty stack [] indicates an idle thread and is treated as a valid distinct call stack.
  • Frame names consist of ASCII alphanumeric characters and underscores ($1 \le \text{len}(frame) \le 64$).

Examples

Example 1

Input: stack_profiler(['record_sample', 'record_sample', 'record_sample', 'record_sample', 'get_top_overall', 'get_top_per_thread'], [['worker-1', ['main', 'physics']], ['worker-2', ['main', 'render']], ['worker-1', ['main', 'physics']], ['worker-2', ['main', 'physics']], [], []])

Output: [None, None, None, None, [['main', 'physics'], 3], {'worker-1': [['main', 'physics'], 2], 'worker-2': [['main', 'physics'], 1]}]

Example 2

Input: stack_profiler(['get_top_overall', 'get_top_per_thread'], [[], []])

Output: [None, {}]

Hints

Hint 1

Since lists are mutable and unhashable in Python, convert incoming stack lists to immutable tuples before using them as keys in standard hash maps or Counter objects.

Hint 2

When resolving ties, you want to maximize frequency while minimizing lexicographical order. The tuple key `(-frequency, stack_tuple)` neatly satisfies both conditions simultaneously using standard sorting or min operations.

Hint 3

Empty stacks `[]` map to `()` in Python. Python compares empty tuples as smaller than any non-empty tuple, perfectly respecting the tie-breaking rule.