← Questions

Exclusive time of functions

0
DecagonUberMetaCodingLast asked May 2026

A coding interview question asked in Meta's technical phone screen and at Uber and Decagon, last asked May 2026. It tests stacks, profiling and parsing.

A single-threaded CPU runs functions with ids 0 to n - 1. Functions call other functions, so calls nest like a stack. You get the start and end log of every call in time order. Return, for each function, its exclusive time: the time spent running its own body, not counting the time spent in the functions it called.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • exclusive_time(n: int, logs: list[str]) -> list[int] — each log is "id:start:t" or "id:end:t". A start begins at the start of time unit t; an end finishes at the end of unit t, so a call that starts and ends at 3 ran for 1 unit. Returns a list of length n.

Example

exclusive_time(2, ["0:start:0", "1:start:2", "1:end:5", "0:end:6"])
  -> [3, 4]          # 0 runs 0-1 and 6, 1 runs 2-5

exclusive_time(3, ["0:start:0", "1:start:1", "2:start:3", "2:end:3",
                   "1:end:6", "0:end:9"])
  -> [4, 5, 1]       # 0: 0 and 7-9; 1: 1-2 and 4-6; 2: 3

exclusive_time(1, ["0:start:0", "0:start:2", "0:end:4", "0:end:7"])
  -> [8]             # a recursive call adds to the same function

Constraints

  • 1 <= n <= 100, 1 <= len(logs) <= 500 (or up to 10^5 for a streaming version).
  • Logs are well formed: properly nested, every start has its end, timestamps never decrease.

Clarifying questions

  • Is an end timestamp inclusive (the function runs through the end of that unit)?
  • Can a function call itself, and should recursive time add up under one id?
  • Can two events share a timestamp?
  • Should I validate malformed logs (an end without a start, wrong nesting) or trust them?

What a strong answer covers

  • A stack of running function ids and the time the current segment started (prev).
  • On start: charge t - prev to the function on top of the stack, push, set prev = t.
  • On end: charge t - prev + 1 to the popped function and set prev = t + 1. The + 1 is the whole difficulty.
  • O(len(logs)) time, O(depth) extra space.
  • Edge cases: recursion, back-to-back calls, a zero-length gap between calls, a single call.

Follow-ups

  • Logs from several threads are interleaved, each line tagged with a thread id.
  • Return inclusive time as well, or the full call tree with time per call path (a flame graph).
  • The logs may be malformed. Detect and report the first bad line.
  • Logs stream in and never end. Report exclusive times so far at any moment.

Where it's asked

  • Meta · Technical phone screen · Last asked May 202645 min · Two coding problems in CoderPad, about 20 minutes each · Usually no AI; some newer screens are AI-assisted
  • Uber · Technical phone screen · Last asked Apr 202660 min · One medium problem with a follow-up, or two short ones; sometimes object-oriented design
  • Decagon · Technical phone screen · Last asked Mar 202645 min · Live coding in a CoderPad-style editor

What it tests

stacks · profiling · parsing

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding