Count business days excluding holidays
Count Working Days Between Two Dates
Given a start date, an end date (inclusive), and a set of holiday dates, compute the number of working days.
A working day is defined as a date that:
- Is not a Saturday or Sunday.
- Is not present in the provided holiday set.
Input
startDate: A string inYYYY-MM-DDformat.endDate: A string inYYYY-MM-DDformat. It is guaranteed thatendDate >= startDate.holidays: A list of distinct date strings, each inYYYY-MM-DDformat.
Output
- An integer representing the total number of working days within the
[startDate, endDate]range.
Example
Given:
startDate = 2026-12-24endDate = 2026-12-28holidays = [2026-12-25]
The dates in the range are:
- 12/24 Thursday (Working day)
- 12/25 Friday (Holiday, not a working day)
- 12/26 Saturday (Weekend, not a working day)
- 12/27 Sunday (Weekend, not a working day)
- 12/28 Monday (Working day)
Output: 2
Constraints
- The date range length (
endDate - startDate + 1) can be up to10^6days. Design an approach appropriate to this constraint, implying that a naive day-by-day iteration might be too slow. - The number of holidays can be up to
10^5.
AI interview prep
Core dilemma
Balancing the need for precise date arithmetic (handling weekends and holidays) with the performance requirements for potentially very large date ranges and holiday sets, avoiding a naive O(D) iteration where D is the number of days.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Count business days excluding holidays
Count Working Days Between Two Dates
Given a start date, an end date (inclusive), and a set of holiday dates, compute the number of working days.
A working day is defined as a date that:
- Is not a Saturday or Sunday.
- Is not present in the provided holiday set.
Input
startDate: A string inYYYY-MM-DDformat.endDate: A string inYYYY-MM-DDformat. It is guaranteed thatendDate >= startDate.holidays: A list of distinct date strings, each inYYYY-MM-DDformat.
Output
- An integer representing the total number of working days within the
[startDate, endDate]range.
Example
Given:
startDate = 2026-12-24endDate = 2026-12-28holidays = [2026-12-25]
The dates in the range are:
- 12/24 Thursday (Working day)
- 12/25 Friday (Holiday, not a working day)
- 12/26 Saturday (Weekend, not a working day)
- 12/27 Sunday (Weekend, not a working day)
- 12/28 Monday (Working day)
Output: 2
Constraints
- The date range length (
endDate - startDate + 1) can be up to10^6days. Design an approach appropriate to this constraint, implying that a naive day-by-day iteration might be too slow. - The number of holidays can be up to
10^5.