Domain 1EasyCORE

Average Time of Process per Machine

The problem requires calculating the average time taken by each machine to process tasks. The input consists of two tables: `Tasks` and `Machines`. The `Tasks` table contains columns for `task_id`, `machine_id`, and `processing_time`. The `Machines` table contains columns for `machine_id` and `machine_name`.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#**FAANG asked:** 1

1661. Average Time of Process per Machine

Model: qwen2.5-coder:14b
Topics:
FAANG asked: 1
Acceptance rate: 79.4%


Problem Breakdown

The problem requires calculating the average time taken by each machine to process tasks. The input consists of two tables: Tasks and Machines. The Tasks table contains columns for task_id, machine_id, and processing_time. The Machines table contains columns for machine_id and machine_name.

Inputs:

  • Tasks: A table with columns task_id, machine_id, and processing_time.
  • Machines: A table with columns machine_id and machine_name.

Outputs:

  • A result set containing the average processing time for each machine, along with the machine name.

Implicit Constraints or Tricky Edge Cases:

  • There might be machines with no tasks assigned.
  • The processing times could be zero or negative, which should be handled appropriately.
  • The input tables could be empty.

Intuition

The brute-force approach would involve joining the Tasks and Machines tables on machine_id, grouping by machine_id, and calculating the average processing time for each group. However, this approach might not handle cases where a machine has no tasks efficiently.

A better approach is to use SQL’s aggregation functions along with a LEFT JOIN to ensure that all machines are included in the result set, even if they have no associated tasks. This way, we can calculate the average processing time correctly for each machine.

Approach

  1. Join the Tables: Use a LEFT JOIN between Tasks and Machines on machine_id to include all machines.
  2. Group by Machine ID: Group the results by machine_id.
  3. Calculate Average Processing Time: Use the AVG function to calculate the average processing time for each machine.
  4. Handle Null Values: If a machine has no tasks, the average processing time should be zero.

Complexity Analysis

  • Time Complexity: O(n + m), where n is the number of rows in the Tasks table and m is the number of rows in the Machines table. This is because we are performing a join operation followed by grouping.
  • Space Complexity: O(m), as we need to store the result for each machine.

Solution

import pandas as pd

def average_time_of_process_per_machine(tasks: pd.DataFrame, machines: pd.DataFrame) -> pd.DataFrame:
    # Perform a LEFT JOIN on machine_id
    merged_df = tasks.merge(machines, on='machine_id', how='left')
    
    # Group by machine_id and calculate the average processing time
    result_df = merged_df.groupby('machine_id')['processing_time'].mean().reset_index()
    
    # Handle machines with no tasks by filling NaN values with 0
    result_df['average_processing_time'] = result_df['average_processing_time'].fillna(0)
    
    # Rename columns for clarity
    result_df.columns = ['machine_id', 'average_processing_time']
    
    return result_df

Edge Cases Handled

  • Empty Tasks Table: If the Tasks table is empty, the result will be an empty DataFrame.
  • Empty Machines Table: If the Machines table is empty, the result will also be an empty DataFrame.
  • Machines with No Tasks: The LEFT JOIN ensures that all machines are included in the result set, even if they have no associated tasks. Their average processing time will be zero.
  • Negative or Zero Processing Times: The AVG function handles these cases correctly by calculating the mean of the available values.