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 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 NameTypeDescription
emp_idintEmployee identifier
event_daydateDate of the office visit
in_timeintEntry time in minutes (1-1440)
out_timeintExit 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
Office Badge System Time TrackingOffice BuildingBadge Scanner๐Ÿ‘คEmployeeTime Records09:00 - Badge In12:00 - Badge Out (Lunch)13:00 - Badge In15:30 - Badge Out (Meeting)16:00 - Badge In18:00 - Badge OutDaily Calculation3h + 1.5h + 2h = 6.5 hours(12-9) + (15.5-13) + (18-16)Final ReportEmployee ID: 1Date: 2020-11-28Total Time: 390 minutesSessions: 3โœ… Payroll ReadySQL SolutionSELECTemp_id,event_day as day, SUM(out_time - in_time)astotal_timeFROMEmployeesGROUP BYemp_id, event_day-- Key insight!
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

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Where k is the number of unique (employee, day) combinations

n
2n
โœ“ 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
Asked in
Amazon 25 Microsoft 18 Google 15 Meta 12
28.4K Views
Medium Frequency
~8 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