Given a string date
representing a Gregorian calendar date formatted as YYYY-MM-DD
, return the day number of the year.
Example 1:
Input: date = "2019-01-09" Output: 9 Explanation: Given date is the 9th day of the year in 2019.
Example 2:
Input: date = "2019-02-10" Output: 41
Constraints:
date.length == 10
date[4] == date[7] == '-'
, and all other date[i]
's are digitsdate
represents a calendar date between Jan 1st, 1900 and Dec 31st, 2019.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 figure out which day of the year a given date is. The brute force method involves manually counting the days. We start from the beginning of the year and add days until we reach the given date.
Here's how the algorithm would work step-by-step:
def day_of_the_year_brute_force(year_string, month_string, day_string):
year = int(year_string)
month = int(month_string)
day = int(day_string)
day_of_year = 0
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# Check if it's a leap year
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
days_in_month[1] = 29
# Sum the days of previous months
for month_index in range(month - 1):
day_of_year += days_in_month[month_index]
# Add the days of the current month
day_of_year += day
return day_of_year
To find the day of the year, we'll calculate how many days have passed in each month before the given date. A key optimization is to pre-calculate the number of days in each month and account for leap years efficiently.
Here's how the algorithm would work step-by-step:
def day_of_the_year(date):
year = int(date[:4])
month = int(date[5:7])
day = int(date[8:])
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# Adjust for leap year.
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
days_in_month[1] = 29
total_days = 0
# Sum days from previous months
for i in range(month - 1):
total_days += days_in_month[i]
# Add the day of the current month.
total_days += day
return total_days
Case | How to Handle |
---|---|
Date string is null or empty | Return an appropriate error code or exception indicating invalid input. |
Date string is in an invalid format | Catch any parsing exceptions and return an error indicating the incorrect date format. |
Invalid year (e.g., negative year or year 0) | Validate the year is a positive integer and return an error if not. |
Invalid month (e.g., month 0 or month > 12) | Validate the month is within the range of 1 to 12 and return an error if not. |
Invalid day (e.g., day 0 or day greater than the maximum days for the given month) | Validate the day is within the valid range for the given month and year, considering leap years, and return an error if not. |
Leap year (February 29th) | Account for February having 29 days in leap years by correctly determining if the year is a leap year and adjusting the days accordingly. |
Maximum valid date (e.g., '9999-12-31') | Ensure the algorithm handles the largest possible valid date without overflow or incorrect calculations. |
Integer overflow in day calculation | Use appropriate data types (e.g., long) to prevent potential integer overflow during the summation of days. |