Given an integer month, return the number of days in that month.
Return 28 for February.
Example 1:
Input: month = 1
Output: 31
Explanation: The month of January has 31 days.
Example 2:
Input: month = 2
Output: 28
Explanation: The month of February has 28 days.
Example 3:
Input: month = 4
Output: 30
Explanation: The month of April has 30 days.
Constraints:
1 <= month <= 12When 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 problem asks us to find the number of days in a given month. A brute-force approach is to simply check each month one-by-one and return the corresponding number of days. This means we will check January, then February, then March, and so on, until we find the month we are looking for.
Here's how the algorithm would work step-by-step:
def number_of_days_in_a_month_brute_force(month, is_leap_year): if month == "January": return 31
if month == "February": # Need to check if it's a leap year to determine February's days
if is_leap_year: return 29 else: return 28
if month == "March": return 31
if month == "April": return 30
if month == "May": return 31
if month == "June": return 30
if month == "July": return 31
if month == "August": return 31
if month == "September": return 30
if month == "October": return 31
if month == "November": return 30
if month == "December": # December is the last month, so return 31 if it matches
return 31To find the number of days in a given month efficiently, we'll use a direct lookup method. We'll take advantage of the fact that the number of days in each month is fixed, except for February in leap years.
Here's how the algorithm would work step-by-step:
def number_of_days_in_a_month(month, year):
# Month values are 1-12
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def is_leap_year(year_number):
if (year_number % 4 == 0 and year_number % 100 != 0) or (year_number % 400 == 0):
return True
else:
return False
# Leap year needs to be checked first.
if is_leap_year(year):
if month == 2:
return 29
# Handle February in Non-Leap Year.
if month == 2:
return 28
# Use lookup for other months.
return days_in_month[month - 1]| Case | How to Handle |
|---|---|
| Invalid month value (e.g., month = 0, month = 13) | Return an error code or throw an exception indicating an invalid month. |
| Non-integer input for month or year | Type check the inputs and return an error or throw an exception if they are not integers. |
| Year is not a valid year (e.g., year = 0 or year is a very small negative number) | Define a reasonable lower bound for the year (e.g., 1) and return an error if it's less than this bound. |
| Leap year (year divisible by 4, but not divisible by 100 unless also divisible by 400) | Specifically handle February correctly, adding a day if it's a leap year. |
| Very large year value that could cause calculations problems | Ensure the year is within a reasonable range and handle potential integer overflow during leap year calculations. |
| The year is before the Gregorian calendar adoption | If the year is prior to 1583, indicate invalid input, or choose a proleptic Gregorian calendar. |
| Negative month | Return an error indicating that the month must be a positive integer. |
| Input is a string or float and is not an integer | Validate the input type and if it is not an integer, return an error. |