Given a date, return the corresponding day of the week for that date.
The input is given as three integers representing the day, month and year respectively.
Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.
Example 1:
Input: day = 31, month = 8, year = 2019 Output: "Saturday"
Example 2:
Input: day = 18, month = 7, year = 1999 Output: "Sunday"
Example 3:
Input: day = 15, month = 8, year = 1993 Output: "Sunday"
Constraints:
1971 and 2100.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to find the day of the week for a given date. With a brute force approach, we'll simply count forward or backward from a known date (like January 1, 1971, which was a Friday) one day at a time until we reach the target date, keeping track of which day of the week we're on.
Here's how the algorithm would work step-by-step:
def day_of_the_week(day, month, year):
known_year = 1971
known_day_of_week = 4 # Friday
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
total_days = 0
# Calculate days from 1971 to the given year
for current_year in range(known_year, year):
if is_leap_year(current_year):
total_days += 366
else:
total_days += 365
# Add days for the months in the given year
for current_month in range(month - 1):
total_days += days_in_month[current_month]
if is_leap_year(year) and current_month == 1:
total_days += 1
total_days += day - 1
# Adjust for the known day of the week
day_index = (known_day_of_week + total_days) % 7
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
return days[day_index]
def is_leap_year(year):
# Account for leap years
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
return True
return FalseThe best way to find the day of the week for any given date involves using a known formula that efficiently calculates the day based on the date's components. Instead of counting day by day from a known date, this formula leverages mathematical properties to jump directly to the solution. We need to break down the date into year, month, and day components and apply the magic formula.
Here's how the algorithm would work step-by-step:
def day_of_the_week(day, month, year):
day_list = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
# Adjust month and year for Jan/Feb
if month < 3:
month += 12
year -= 1
year_of_century = year % 100
century = year // 100
# Zeller's Congruence formula
# This formula is the core logic for calculating the day
day_index = (day + (13 * (month + 1)) // 5 + year_of_century + (year_of_century // 4) + (century // 4) - (2 * century)) % 7
# Python's modulo can return negative numbers, so adjust.
day_index = (day_index + 7) % 7
# Return the day of the week
return day_list[day_index]| Case | How to Handle |
|---|---|
| Invalid date components (e.g., month 0, day 0, year -1) | Handle invalid date components by either returning an error message or defaulting to a sensible default date (e.g., January 1, 1900) and documenting the behavior. |
| Dates outside the range supported by the algorithm (e.g., before the Gregorian calendar was adopted) | Check if the input date is within the supported range and return an error or a predefined result if outside. |
| Leap years and their impact on the day of the week calculation, especially around February 29th | Ensure the algorithm correctly accounts for leap years by incorporating leap year logic in the date calculations. |
| Dates close to integer limits (potential overflow during calculations) | Use appropriate data types (e.g., long) and modular arithmetic to prevent integer overflow during calculations. |
| Ambiguous date formats (e.g., mm/dd/yyyy vs dd/mm/yyyy) | Specify the expected date format and validate the input accordingly, returning an error if the format is incorrect. |
| Epoch year is a leap year (Year 0000 being invalid) | Do not assume year 0 to be a valid leap year, and use the correct offset to calculate the Julian day. |
| Gregorian calendar reform date | If the algorithm spans dates before and after the Gregorian reform, incorporate logic to handle the change in calendar systems. |
| Very large year values that could introduce precision errors or performance issues | Ensure that the algorithm handles large year values gracefully without introducing precision errors or performance degradation. |