Katkat

Find Peak Memory Usage Across Process Intervals

NutanixSoftware Engineer · Technical Screen

Last updated

Calculate the peak simultaneous memory usage from a set of half-open intervals and identify the earliest continuous time window sustaining that peak.

Problem Statement

You are monitoring system resources in a virtualized infrastructure. A hypervisor records the memory allocation of various processes over time. Each process is represented as a tuple (start, end, memory), indicating that it consumes memory units of RAM throughout the half-open time interval [start, end) (active at time t if and only if start <= t < end).

Your task is to calculate the maximum total memory consumed simultaneously at any point in time, and identify the earliest positive-duration interval [peak_start, peak_end) during which this peak memory was continuously sustained.

Key rules to consider:

  • Intervals are half-open: a process ending at time t releases its memory at t, while a process starting at time t claims its memory at t. These occur at the exact same moment.
  • If multiple adjacent segments sustain the maximum memory consecutively without dropping below it, they should be merged into a single contiguous interval [peak_start, peak_end).
  • If the peak is reached across multiple disjoint intervals, return the interval that starts the earliest.
  • If the input is empty, return (0, 0, 0).

Function Signature

def find_peak_memory(processes: list[tuple[int, int, int]]) -> tuple[int, int, int]:
    """
    Returns (peak_memory, peak_start, peak_end).
    """
    pass

Examples

Example 1

processes = [
    (1, 5, 10),
    (2, 4, 20),
    (3, 6, 15)
]
find_peak_memory(processes)
# Output: (45, 3, 4)

Explanation:

  • [1, 2): Memory is 10
  • [2, 3): Memory is 10 + 20 = 30
  • [3, 4): Memory is 10 + 20 + 15 = 45 (Peak)
  • [4, 5): Process 2 ends, memory drops to 10 + 15 = 25
  • [5, 6): Memory is 15

Example 2

processes = [
    (1, 4, 10),
    (4, 7, 10),
    (2, 5, 10)
]
find_peak_memory(processes)
# Output: (20, 2, 5)

Explanation: At t = 4, the first process ends (-10) and the second starts (+10), keeping memory at 20. The peak of 20 is sustained continuously from t = 2 to t = 5.

Constraints

  • 0 <= len(processes) <= 2 * 10^5
  • 0 <= start < end <= 10^9
  • 1 <= memory <= 10^9
  • Memory sums can exceed standard 32-bit integers.

Hints

Hint 1

Transform each process into two point events: a start event (+memory) and an end event (-memory), then sort all unique event timestamps.

Hint 2

Because intervals are half-open, combine all memory deltas that occur at the exact same timestamp before evaluating the memory level of the segment [t_i, t_{i+1}).

Hint 3

Track the current continuous peak segment across adjacent time intervals and merge them if the peak is sustained across consecutive boundaries.

Follow-up questions

  • How would you adapt this solution for a streaming system where process events arrive out of order in real time?
  • If the dataset contains hundreds of millions of processes that cannot fit into memory on a single machine, how would you compute the peak using MapReduce or distributed engines?
  • How would your approach change if memory usage wasn't constant, but rather ramped up linearly from start to end?