Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Math
Average Time Of Process Per Machine

Compute the average processing time for each machine from paired start and end logs.

Acceptance 100%
Problem Statement

You are given a list of process log records. Each record belongs to a machine and contains a process id, the machine id, a timestamp, and an event type indicating whether the process started or ended.

For every machine, consider all of its completed processes and compute the average processing time as:

average=sum of all durationsnumber of completed processes\text{average} = \frac{\text{sum of all durations}}{\text{number of completed processes}}

Return the result for each machine, typically ordered by machine id.

A process duration is the difference between its end timestamp and start timestamp for the same process id on the same machine.

Input Format

  • A list of log records.
  • Each record contains:
    • process id
    • machine id
    • timestamp
    • event type (start or end)
  • Every completed process has exactly one start and one end record.

Output Format

  • Return the average processing time for each machine.
  • Each result should identify the machine and its average duration.

Constraints

  • Each process id is associated with one machine.
  • Timestamps for a process are valid and allow duration calculation as end - start.
  • Only completed processes contribute to the average.
  • Use exact arithmetic or rounded output as required by the platform format.
Examples
Sample cases returned by the problem API.

Example 1

Input

logs = [[1,1,5,"start"],[1,1,10,"end"],[2,1,12,"start"],[2,1,18,"end"],[3,2,7,"start"],[3,2,9,"end"]]

Output

[[1,6],[2,2]]

Explanation

Machine 1 has durations 5 and 6, so its average is 11 / 2 = 5.5. If the platform requires integer output, it may be represented according to the specific format. Machine 2 has one duration of 2.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.