This Employee Management System is designed to efficiently track and manage employee work shifts in a factory environment. It handles multiple and overlapping shifts by merging them to determine the longest continuous working period. The system also calculates total working hours and identifies overtime when an employee exceeds the standard 8-hour shift. Overtime pay is then computed at 1.5 times the regular hourly rate. Overall, the system ensures accurate payroll processing, fair compensation, and improved workforce management. For more useful resources visit GeeksCodes for detailed guides and tutorials.
Employee Shift Scheduling And Overtime Calculation In JavaScript
![]() |
| Master Employee Shift Tracking & Overtime Pay with JavaScript |
In a factory environment, employees often work across multiple shifts throughout the day and night. These shifts may vary in duration and can sometimes overlap due to operational requirements, shift transitions, or unexpected workload demands. Because of this complexity, it becomes essential to accurately track and manage all employee working hours to ensure proper payroll processing and compliance with company policies.
One of the key challenges in such a system is handling overlapping shifts. When two or more shifts overlap, they must be merged intelligently to avoid double-counting hours and to determine the actual continuous working period. By combining these overlapping intervals, the system can correctly calculate the longest uninterrupted shift worked by an employee. This is especially important for monitoring fatigue, ensuring workplace safety, and maintaining productivity standards.
In addition to tracking shift durations, the company has a policy for overtime compensation. Any employee who works beyond the standard 8-hour shift is eligible for overtime pay. Therefore, once the total working hours for a shift (after merging overlaps) are calculated, the system must identify the extra hours worked beyond this threshold. These additional hours are then used to compute overtime pay based on predefined rates set by the organization.
To address these requirements, you are developing a robust Employee Management System that can efficiently:
- Record and store multiple shifts for each employee
- Detect and merge overlapping shifts accurately
- Calculate the longest continuous working shift
- Compute total working hours per day or per shift
- Identify overtime hours beyond 8 hours
- Calculate overtime pay according to company rules
This system not only ensures fair and accurate compensation for employees but also helps management maintain transparency, optimize workforce scheduling, and improve overall operational efficiency.
The system needs to perform two tasks:
- Employee Shift Scheduling: Given an array of employee shifts, each with a start time and end time, determine the longest shift an employee worked. The shifts can overlap, and we need to merge them to find the longest continuous work period.
- Overtime Calculation: If the employee works more than 8 hours in a single shift, calculate the overtime pay. The overtime rate is 1.5 times the regular hourly rate.
Your task is to build a system that:
- Calculates the longest continuous shift (by merging overlapping shifts).
- Computes the total overtime pay based on shifts longer than 8 hours.
Input Format:
- The first input is an integer
n, the number of shifts. - The next
nlines contain the shift details, with each shift represented by two integers: the start time and end time of the shift. - For overtime calculation, you are also provided an integer
hourly rate, which is the regular hourly rate for overtime computation.
Output Format:
- The output should contain:
longestShift: The duration of the longest continuous shift in hours.overtimePay: The total overtime pay calculated based on the overtime rate.
1)
Sample Input
4
1 5
3 7
8 12
5 9
50
Sample Output
{ longestShift: 11, overtimePay: 225 }
2)
Sample Input
3
0 3
5 8
9 13
25
Sample Output
{ longestShift: 4, overtimePay: 0 }
Code Will Be JavaScript
function mergeShifts(shifts) {
// Sort shifts by start time
shifts.sort((a, b) => a[0] - b[0]);
const merged = [shifts[0]];
for (let i = 1; i < shifts.length; i++) {
let last = merged[merged.length - 1];
let current = shifts[i];
if (current[0] <= last[1]) {
// Overlapping shift: merge
last[1] = Math.max(last[1], current[1]);
} else {
merged.push(current);
}
}
return merged;
}
function calculateOvertimeAndLongestShift(n, shiftData, hourlyRate) {
// Merge shifts for longest shift
const mergedShifts = mergeShifts(shiftData);
let longestShift = 0;
let overtimePay = 0;
for (const [start, end] of mergedShifts) {
const duration = end - start;
longestShift = Math.max(longestShift, duration);
}
// Calculate overtime on original unmerged shifts
for (const [start, end] of shiftData) {
const duration = end - start;
if (duration > 8) {
const overtimeHours = duration - 8;
overtimePay += overtimeHours * hourlyRate * 1.5;
}
}
return {
longestShift,
overtimePay
};
}
// Sample Input Handling
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let inputLines = [];
rl.on("line", function (line) {
inputLines.push(line);
});
rl.on("close", function () {
const n = parseInt(inputLines[0]);
const shiftData = [];
for (let i = 1; i <= n; i++) {
const [start, end] = inputLines[i].split(" ").map(Number);
shiftData.push([start, end]);
}
const hourlyRate = parseInt(inputLines[n + 1]);
const result = calculateOvertimeAndLongestShift(n, shiftData, hourlyRate);
console.log(result);
});
Frequently Asked Questions
Q1: What is a shift overlap?
An overlap occurs when the time period of one shift intersects with another, such as one ending after another begins.
Q2: How is the longest shift determined?
The system merges all overlapping shifts and identifies the one with the maximum duration as the longest continuous shift.
Q3: What qualifies as overtime?
Any shift where the employee works more than 8 hours is considered for overtime. Only the hours beyond 8 are used for overtime calculation.
Q4: How is the overtime pay calculated?
Overtime pay = Overtime hours × Hourly rate × 1.5
Q5: Why are merged shifts not used for overtime?
Overtime is calculated per actual shift worked (not merged), so each shift's duration is considered individually to compute overtime.
![]() |
| Master Employee Shift Tracking & Overtime Pay with JavaScript |


