cswork / libraryOpen practice workspace

Interview questions · Meta

Compute interval mode, BST range sum, exclusive time

This multipart question evaluates proficiency in interval coverage and frequency analysis, binary search tree range-sum queries with preprocessing for repeated queries, and exclusive-time computation from call logs, covering concepts such as event counting/sweep-line, BST traversal and augmentation, and nested-call time accounting.

You are given several independent coding tasks.

A) Most frequent integer covered by intervals

You are given an integer range [-M, M] and a list of inclusive integer intervals, e.g. [1, 3] means the integers {1,2,3} are each covered once by that interval. Multiple intervals may overlap.

Return any integer in [-M, M] that has the maximum total coverage count (i.e., appears in the most intervals when expanded to integers). If multiple integers tie for the maximum, returning any one of them is fine.

Clarify assumptions/constraints you need (e.g., size of M, number of intervals, endpoints within [-M, M]).


B) Range sum on a BST + follow-up optimization

Given the root of a Binary Search Tree (BST) and two integers low and high, return the sum of all node values v such that low <= v <= high.

Follow-up: Suppose the tree is very large and you need to answer many queries with different [low, high] ranges. What preprocessing or data structure would you use to make queries faster?


C) Exclusive execution time from logs

There are n functions labeled 0..n-1. You are given a list of logs in chronological order. Each log is a string formatted as:

"<functionId>:start:<timestamp>" or "<functionId>:end:<timestamp>"

  • start means the function starts at that timestamp.
  • end means the function ends at that timestamp (inclusive).
  • Functions may be nested due to calls.

Return an array ans of length n where ans[i] is the exclusive time spent in function i (time in i excluding time in any functions it calls).

State any needed assumptions (e.g., timestamps are integers, single-threaded execution).

Community answers

Nothing here yet.

Join in on CSWork →