Employee Shift Scheduling And Overtime Calculation In JavaScript
![]() |
Master Employee Shift Tracking & Overtime Pay with JavaScript |
In a factory environment, employees work multiple shifts. Some shifts overlap with each other, so it is important to combine these overlapping shifts to calculate the longest shift accurately. Additionally, the company wants to calculate overtime pay for employees working beyond 8 hours.
You need to track the shifts, calculate the longest shift worked, and then determine how much overtime each employee has earned based on the company policy.
You are developing an Employee Management System for a company that tracks employee shifts and calculates overtime pay.
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
n
lines 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 (FAQs)
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 |