KatInterview

Reconcile Employee Project Hours Across Two Tables

SQL & DataAuto-graded · 13 test cases
MediumAuto-gradedSQL & Datadata manipulationjoiningaggregation

Last updated

Tests the ability to join two lists of dicts (employees and time-log entries) and compute per-employee aggregated project statistics, mimicking a SQL left join + GROUP BY.

Reconcile Employee Project Hours Across Two Tables

You are given two tables represented as lists of dictionaries.

employees — one row per employee:

fieldtypedescription
employee_idintunique identifier
namestrfull name
departmentstrdepartment name

logs — one row per work-log entry (an employee may have many entries, or none):

fieldtypedescription
employee_idintreferences an employee
projectstrproject code (non-empty string)
hoursfloathours worked (always > 0)

Your task is to produce a summary report — one record per employee — equivalent to this SQL:

SELECT e.employee_id,
       e.name,
       e.department,
       COUNT(DISTINCT l.project) AS num_projects,
       COALESCE(SUM(l.hours), 0.0)  AS total_hours,
       COALESCE(MAX(l.hours), 0.0)  AS max_single_log
FROM employees e
LEFT JOIN logs l ON e.employee_id = l.employee_id
GROUP BY e.employee_id, e.name, e.department

Return a list of dicts, one per employee, each with exactly these keys:

  • "employee_id" (int)
  • "name" (str)
  • "department" (str)
  • "num_projects" (int) — number of distinct project codes logged by this employee
  • "total_hours" (float) — sum of all hours logged; 0.0 if no logs
  • "max_single_log" (float) — maximum hours in a single log entry; 0.0 if no logs

Ordering

Sort the result primarily by department (ascending, case-sensitive lexicographic), secondarily by employee_id (ascending).

Notes

  • employees contains no duplicate employee_id values.
  • logs may reference only a subset of employees; employees with no logs still appear in the output.
  • logs may contain multiple entries for the same (employee_id, project) pair — all such entries contribute to total_hours and max_single_log; the project is counted only once in num_projects.
  • An employee_id that appears in logs but not in employees should be silently ignored.
  • total_hours and max_single_log must be plain Python float values (not int).

Constraints

  • 0 ≤ len(employees) ≤ 2000
  • 0 ≤ len(logs) ≤ 10000
  • 1 ≤ employee_id ≤ 10^6
  • 0.0 < hours ≤ 500.0
  • Department names and project codes are non-empty strings of printable ASCII characters.

Examples

Example 1

Input: reconcile_hours([{'employee_id': 1, 'name': 'Alice', 'department': 'Engineering'}, {'employee_id': 2, 'name': 'Bob', 'department': 'Marketing'}, {'employee_id': 3, 'name': 'Carol', 'department': 'Engineering'}], [{'employee_id': 1, 'project': 'Alpha', 'hours': 3}, {'employee_id': 1, 'project': 'Beta', 'hours': 5}, {'employee_id': 1, 'project': 'Alpha', 'hours': 2}, {'employee_id': 3, 'project': 'Beta', 'hours': 8}])

Output: [{'employee_id': 1, 'name': 'Alice', 'department': 'Engineering', 'num_projects': 2, 'total_hours': 10, 'max_single_log': 5}, {'employee_id': 3, 'name': 'Carol', 'department': 'Engineering', 'num_projects': 1, 'total_hours': 8, 'max_single_log': 8}, {'employee_id': 2, 'name': 'Bob', 'department': 'Marketing', 'num_projects': 0, 'total_hours': 0, 'max_single_log': 0}]

Example 2

Input: reconcile_hours([{'employee_id': 10, 'name': 'Dan', 'department': 'HR'}], [])

Output: [{'employee_id': 10, 'name': 'Dan', 'department': 'HR', 'num_projects': 0, 'total_hours': 0, 'max_single_log': 0}]

Example 3

Input: reconcile_hours([{'employee_id': 5, 'name': 'Eve', 'department': 'Finance'}, {'employee_id': 6, 'name': 'Frank', 'department': 'Finance'}], [{'employee_id': 5, 'project': 'X', 'hours': 4.5}, {'employee_id': 5, 'project': 'X', 'hours': 1.5}, {'employee_id': 99, 'project': 'Y', 'hours': 7}])

Output: [{'employee_id': 5, 'name': 'Eve', 'department': 'Finance', 'num_projects': 1, 'total_hours': 6, 'max_single_log': 4.5}, {'employee_id': 6, 'name': 'Frank', 'department': 'Finance', 'num_projects': 0, 'total_hours': 0, 'max_single_log': 0}]

Hints

Hint 1

Build a dictionary keyed by employee_id from the logs list before iterating over employees — this gives O(1) lookup and avoids a nested loop.

Hint 2

Use a nested dict or collections.defaultdict to accumulate, per employee, the set of distinct projects plus running sum and running max of hours.

Hint 3

Remember the LEFT JOIN semantics: every employee must appear in the output even if they have zero matching log rows — handle the missing-key case explicitly.