InterviewDB Experience · USA

Function Time: Compute Exclusive Execution Time of Each Process

Interview Experience

Problem

You have a single-threaded CPU running n functions (0-indexed). You are given a log of start and end events. Compute the exclusive execution time of each function (time spent in that function, not counting nested calls).

Log format: "function_id:start|end:timestamp"

python
def exclusive_time(n: int, logs: list[str]) -> list[int]:
    """Return list of length n with exclusive time for each function."""
    pass

**Input**:  n = 2
        logs = ["0:start:0","1:start:2","1:end:5","0:end:6"]

**Output**: [3, 4]
# Function 0: runs [0,1] and [6,6] -> 2+1=3
# Function 1: runs [2,5] -> 4

**Input**:  n = 1
        logs = ["0:start:0","0:start:2","0:end:5","0:end:6"]

**Output**: [7]

Follow-ups

  1. How does a stack naturally model the nesting of function calls here?
  2. What edge cases arise with recursive functions (same function ID appears on the stack multiple times)?
  3. If timestamps were floating-point, how would you adjust the interval arithmetic?
  4. Extend to multi-threaded execution: each function has a thread ID — what changes?

Full Details

Problem

You have a single-threaded CPU running n functions (0-indexed). You are given a log of start and end events. Compute the exclusive execution time of each function (time spent in that function, not counting nested calls).

Log format: "function_id:start|end:timestamp"

python
def exclusive_time(n: int, logs: list[str]) -> list[int]:
    """Return list of length n with exclusive time for each function."""
    pass

**Input**:  n = 2
        logs = ["0:start:0","1:start:2","1:end:5","0:end:6"]

**Output**: [3, 4]
# Function 0: runs [0,1] and [6,6] -> 2+1=3
# Function 1: runs [2,5] -> 4

**Input**:  n = 1
        logs = ["0:start:0","0:start:2","0:end:5","0:end:6"]

**Output**: [7]

Follow-ups

  1. How does a stack naturally model the nesting of function calls here?
  2. What edge cases arise with recursive functions (same function ID appears on the stack multiple times)?
  3. If timestamps were floating-point, how would you adjust the interval arithmetic?
  4. Extend to multi-threaded execution: each function has a thread ID — what changes?

About This Question

This is a candidate experience report from a decagon interview during the phone round.

It covers the following topics: Recursion, Coding, Phone, Stack .