Find Total Time Spent by Each Employee - Problem
Employee Time Tracker - Office Hours Calculator
You're building a time tracking system for a company that monitors when employees enter and leave the office. The system records multiple entries and exits throughout the day for each employee.
๐ Your Task:
Calculate the
๐ข The Database Schema:
๐ฏ Goal: Return each employee's total office hours per day by summing up all their
You're building a time tracking system for a company that monitors when employees enter and leave the office. The system records multiple entries and exits throughout the day for each employee.
๐ Your Task:
Calculate the
total time spent by each employee in the office for each day. An employee might have multiple entry-exit sessions on the same day (like leaving for lunch and coming back).๐ข The Database Schema:
| Column Name | Type | Description |
|---|---|---|
emp_id | int | Employee identifier |
event_day | date | Date of the office visit |
in_time | int | Entry time in minutes (1-1440) |
out_time | int | Exit time in minutes (1-1440) |
๐ฏ Goal: Return each employee's total office hours per day by summing up all their
(out_time - in_time) sessions. Input & Output
example_1.sql โ Basic Case
$
Input:
Employees table:
+--------+------------+---------+----------+
| emp_id | event_day | in_time | out_time |
+--------+------------+---------+----------+
| 1 | 2020-11-28 | 4 | 32 |
| 1 | 2020-11-28 | 55 | 200 |
| 1 | 2020-12-03 | 1 | 42 |
| 2 | 2020-11-28 | 3 | 33 |
| 2 | 2020-12-09 | 47 | 74 |
+--------+------------+---------+----------+
โบ
Output:
+--------+------------+------------+
| emp_id | day | total_time |
+--------+------------+------------+
| 1 | 2020-11-28 | 173 |
| 1 | 2020-12-03 | 41 |
| 2 | 2020-11-28 | 30 |
| 2 | 2020-12-09 | 27 |
+--------+------------+------------+
๐ก Note:
Employee 1 worked two sessions on 2020-11-28: (32-4) + (200-55) = 28 + 145 = 173 minutes. Employee 2 worked single sessions on both days: (33-3) = 30 minutes and (74-47) = 27 minutes.
example_2.sql โ Single Sessions
$
Input:
Employees table:
+--------+------------+---------+----------+
| emp_id | event_day | in_time | out_time |
+--------+------------+---------+----------+
| 3 | 2020-11-30 | 60 | 120 |
| 4 | 2020-11-30 | 90 | 150 |
+--------+------------+---------+----------+
โบ
Output:
+--------+------------+------------+
| emp_id | day | total_time |
+--------+------------+------------+
| 3 | 2020-11-30 | 60 |
| 4 | 2020-11-30 | 60 |
+--------+------------+------------+
๐ก Note:
Both employees worked exactly 60 minutes each: (120-60) = 60 and (150-90) = 60.
example_3.sql โ Multiple Days
$
Input:
Employees table:
+--------+------------+---------+----------+
| emp_id | event_day | in_time | out_time |
+--------+------------+---------+----------+
| 5 | 2020-11-28 | 10 | 50 |
| 5 | 2020-11-29 | 20 | 80 |
| 5 | 2020-11-29 | 100 | 140 |
+--------+------------+---------+----------+
โบ
Output:
+--------+------------+------------+
| emp_id | day | total_time |
+--------+------------+------------+
| 5 | 2020-11-28 | 40 |
| 5 | 2020-11-29 | 100 |
+--------+------------+------------+
๐ก Note:
Employee 5 worked 40 minutes on 2020-11-28 and 100 minutes total on 2020-11-29 from two sessions: (80-20) + (140-100) = 60 + 40 = 100 minutes.
Visualization
Tap to expand
Understanding the Visualization
1
Raw Badge Swipes
Every entry/exit creates a record with timestamp
2
Group by Worker & Day
Combine all sessions for same person on same day
3
Calculate Session Times
Find duration of each individual session
4
Sum Daily Totals
Add up all session times for final daily hours
Key Takeaway
๐ฏ Key Insight: Use SQL GROUP BY to aggregate time sessions per employee per day, then SUM the time differences to get total daily hours.
Time & Space Complexity
Time Complexity
O(n)
Single pass through all records with grouping
โ Linear Growth
Space Complexity
O(k)
Where k is the number of unique (employee, day) combinations
โ Linear Space
Constraints
- 1 โค Number of employees โค 100
-
1 โค
in_time<out_timeโค 1440 -
Each
(emp_id, event_day, in_time)combination is unique - Time values represent minutes from midnight (1440 = 24 hours)
- No overlapping sessions for the same employee on the same day
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code