Working Days Between Two Dates with a Closure List
Math & Bit ManipulationAuto-graded · 18 test casesLast updated
Count Monday-to-Friday days in an inclusive date range, skipping listed closure dates, using week arithmetic instead of day-by-day iteration.
Working Days Between Two Dates
You are given two calendar dates, first_day and last_day, and a list of company closure dates. Return how many working days fall in the range from first_day to last_day, with both endpoints included.
A working day is a date that meets both conditions:
- It falls on Monday, Tuesday, Wednesday, Thursday, or Friday.
- It does not appear in the
closureslist.
Rules
- All dates are strings in
YYYY-MM-DDformat. - If
first_dayis later thanlast_day, the range is empty and the answer is0. - If both dates are the same, the answer is
1when that day is a working day, and0otherwise. - A closure date that falls on a Saturday or Sunday has no effect, since weekends are never counted.
- A closure date outside the range has no effect.
- If a closure date is listed more than once, it still removes at most one day.
Function
Implement working_days_between(first_day, last_day, closures), which returns an integer.
Constraints
- Every date is a valid date in
YYYY-MM-DDformat. - The range may span several years, including leap years, so an approach that does not scan every single day is preferable.
closuresmay be empty, may contain duplicates, and may contain weekend dates or dates outside the range.- The return value is a non-negative integer.
Examples
Example 1
Input: working_days_between('2023-12-25', '2023-12-29', ['2023-12-25'])
Output: 4
Example 2
Input: working_days_between('2023-01-01', '2023-01-07', [])
Output: 5
Hints
Hint 1
Any 7 consecutive days contain exactly 5 weekdays, no matter which weekday the block starts on. Use this to count whole weeks in one step.
Hint 2
After removing whole weeks, at most 6 days remain. They begin on the same weekday as the first day, so check each of them directly.
Hint 3
Put closures into a set of parsed dates first. Subtract only those inside the range that fall on a weekday, so duplicates, weekend closures and out-of-range closures are handled correctly.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Split the work into two independent parts.
-
Count weekdays arithmetically.
- Compute the number of days in the inclusive range:
(end - start).days + 1. - Divide by 7 to get whole weeks and a leftover of 0 to 6 days.
- Every whole week has exactly 5 weekdays, so it contributes
5 * weeks. - The leftover days start on the same weekday as
start(shifting by whole weeks does not change the weekday). Check each of them with(start.weekday() + offset) % 7 < 5, where Monday is 0 and Sunday is 6.
- Compute the number of days in the inclusive range:
-
Subtract closures that actually removed a counted day.
- Parse the closures into a set, which removes duplicates.
- Subtract the number of those dates that lie within
[start, end]and fall on a weekday.
If start > end, return 0 immediately.
Why it is correct
- Whole weeks cover
startthroughstart + 7*weeks - 1. Each such block contains each weekday once, so exactly 5 weekdays. The leftover days are tested one by one, so every date in the range is counted exactly once. - A closure should reduce the answer only if its date was counted in step 1: it must be in range and on a weekday. Weekend closures and out-of-range closures were never counted, so they are ignored. The set ensures no date is subtracted twice.
- Using
+ 1makes both endpoints inclusive. When both dates are equal, there are 0 whole weeks and 1 leftover day, giving 1 or 0 as required. datetime.datehandles leap years and multi-year spans.
Complexity
- Time: O(H), where H is the number of closures. The weekday count is O(1), and it does not depend on the length of the range.
- Space: O(H) for the set of closure dates.
Reference solution
from datetime import date
from typing import List
def working_days_between(first_day: str, last_day: str, closures: List[str]) -> int:
start = date.fromisoformat(first_day)
end = date.fromisoformat(last_day)
if start > end:
return 0
total_days = (end - start).days + 1
full_weeks, leftover = divmod(total_days, 7)
result = full_weeks * 5
first_weekday = start.weekday()
for offset in range(leftover):
if (first_weekday + offset) % 7 < 5:
result += 1
closed = {date.fromisoformat(s) for s in closures}
result -= sum(
1 for d in closed
if start <= d <= end and d.weekday() < 5
)
return result