Task 1: Banking Day Offset
You are given a start date and offset counter. Optionally you also get a bank holiday date list.
Given a number (of days) and a start date, return the date adjusted to take into account non-banking days. In other words: convert a banking day offset to a calendar day offset.
Non-banking days are:
- Weekends
- Bank holidays
Example 1: Input: $start_date = '2018-06-28', $offset = 3, $bank_holidays = ['2018-07-03'] → Output: '2018-07-04'
Example 2: Input: $start_date = '2018-06-28', $offset = 3 → Output: '2018-07-03'
Method Explanation
The task requires advancing a cursor date by a specified number of banking days. Starting at $start_date, we iteratively add one calendar day at a time:
- If the current day falls on a weekend (Saturday or Sunday), we skip it.
- If the current day matches any date in the provided bank holiday list (tested in $O(1)$ via a hash/set lookup), we skip it.
- Otherwise, the day qualifies as a valid banking day, so we decrement our offset counter.
- When the remaining offset reaches zero, the current calendar date is returned in
YYYY-MM-DDformat.
Perl Solution
use v5.38;
use warnings;
use Time::Piece;
use Time::Seconds qw(ONE_DAY);
use Type::Params -sigs;
use Types::Standard qw(Str Int ArrayRef Optional);
signature_for banking_day_offset => (
method => 0,
positional => [ Str, Int, Optional [ ArrayRef [Str] ] ],
);
sub banking_day_offset ( $start_date, $offset, $bank_holidays = [] ) {
$bank_holidays //= [];
my %holidays = map { $_ => 1 } @$bank_holidays;
my $current = Time::Piece->strptime( "$start_date 12:00:00 UTC", "%Y-%m-%d %H:%M:%S %Z" );
my $remaining = $offset;
while ( $remaining > 0 ) {
$current += ONE_DAY;
my $ymd = $current->strftime('%Y-%m-%d');
my $wday = $current->wday; # 1 = Sunday, 7 = Saturday
next if $wday == 1 || $wday == 7;
next if $holidays{$ymd};
$remaining--;
}
return $current->strftime('%Y-%m-%d');
}
Python Solution
from datetime import datetime, timedelta
def banking_day_offset(
start_date: str, offset: int, bank_holidays: list[str] | None = None
) -> str:
"""Calculate the target date after advancing a number of banking days."""
holidays = set(bank_holidays or [])
current = datetime.strptime(start_date, "%Y-%m-%d").date()
remaining = offset
one_day = timedelta(days=1)
while remaining > 0:
current += one_day
# weekday(): Monday is 0 and Sunday is 6
if current.weekday() >= 5:
continue
if current.isoformat() in holidays:
continue
remaining -= 1
return current.isoformat()