Employees With Deductions - Problem

Given two tables Employees and Logs, find the IDs of employees who will have deductions from their salary due to insufficient work hours.

Each employee must work a certain number of hours every month. The actual hours worked are calculated from the Logs table by:

  • Summing all work sessions for each employee
  • Each session duration is rounded up to the nearest minute
  • For example: 51 minutes 2 seconds becomes 52 minutes

Return the employee_id of employees whose total worked minutes is less than their required hours (converted to minutes).

Table Schema

Employees
Column Name Type Description
employee_id PK int Unique employee identifier
needed_hours int Minimum hours required per month
Primary Key: employee_id
Logs
Column Name Type Description
employee_id PK int Employee identifier
in_time PK datetime Work session start time
out_time PK datetime Work session end time
Primary Key: (employee_id, in_time, out_time)

Input & Output

Example 1 — Mixed Work Hours
Input Tables:
Employees
employee_id needed_hours
1 10
2 12
Logs
employee_id in_time out_time
1 2022-10-01 09:00:00 2022-10-01 17:30:00
1 2022-10-02 09:00:00 2022-10-02 10:30:00
Output:
employee_id
2
💡 Note:

Employee 1 worked 8.5 hours + 1.5 hours = 10 hours total, meeting the 10-hour requirement.

Employee 2 has no logged work sessions, so 0 hours < 12 hours required, resulting in deductions.

Example 2 — Minute Rounding
Input Tables:
Employees
employee_id needed_hours
3 8
Logs
employee_id in_time out_time
3 2022-10-01 09:00:00 2022-10-01 16:59:30
Output:
employee_id
3
💡 Note:

Employee 3 worked 7 hours 59 minutes 30 seconds, which rounds up to 480 minutes (8 hours).

However, since they need exactly 8 hours and worked slightly less, they receive deductions.

Constraints

  • 1 ≤ employee_id ≤ 10000
  • 1 ≤ needed_hours ≤ 50
  • All times are in October 2022
  • out_time can be one day after in_time

Visualization

Tap to expand
Employees With Deductions INPUT ID Needed Hrs 1 8 2 10 3 6 Work Logs ID In Out 1 09:00 17:30 2 08:00 16:20 2 18:00 18:45 3 10:00 14:30 Each session rounded UP to nearest minute ALGORITHM STEPS 1 Parse Work Logs Calculate duration for each session (Out - In) 2 Round Up Minutes Round each session to next whole minute 3 Sum Per Employee Group logs by ID and sum total work time 4 Compare Hours If worked < needed, add to deduction list Calculation Example (ID=2) Session 1: 8h 20m = 500 min Session 2: 45m = 45 min Total: 545 min = 9h 5m Needed: 10h -- DEDUCTION! FINAL RESULT Work Hours Summary Employee 1 Worked: 8h 30m (510 min) Needed: 8h -- OK Employee 2 Worked: 9h 5m (545 min) Needed: 10h -- DEDUCTION Employee 3 Worked: 4h 30m (270 min) Needed: 6h -- DEDUCTION Output Array [2, 3] Employees with deductions Key Insight: The critical step is rounding UP each work session to the nearest minute before summing. Use a HashMap to accumulate total minutes per employee, then compare with required hours. Time Complexity: O(n) where n = number of work log entries. Space: O(e) for e employees. TutorialsPoint - Employees With Deductions | Optimal Solution
Asked in
Amazon 28 Microsoft 19
23.5K Views
Medium Frequency
~18 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen