Given a date
string in the form Day Month Year
, where:
Day
is in the set {"1st", "2nd", "3rd", "4th", ..., "30th", "31st"}
.Month
is in the set {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
.Year
is in the range [1900, 2100]
.Convert the date string to the format YYYY-MM-DD
, where:
YYYY
denotes the 4 digit year.MM
denotes the 2 digit month.DD
denotes the 2 digit day.Example 1:
Input: date = "20th Oct 2052" Output: "2052-10-20"
Example 2:
Input: date = "6th Jun 1933" Output: "1933-06-06"
Example 3:
Input: date = "26th May 1960" Output: "1960-05-26"
Constraints:
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 brute force method for reformatting a date involves checking all possible valid arrangements. We will have a way to convert the given date string into its components, and then create a valid output date string. We simply test every possible date combination that could be valid and pick the correct one.
Here's how the algorithm would work step-by-step:
def reformat_date(date):
day_string, month_string, year_string = date.split()
month_list = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]
# Find the month number corresponding to month_string
month_number = month_list.index(month_string) + 1
# Convert the day_string to an integer and remove suffix
day_number = int(day_string[:-2])
# Construct the reformatted date string
reformatted_date = f"{year_string}-month_number:02}-day_number:02}"
return reformatted_date
The optimal approach involves converting the given date string into a standardized format. We achieve this by extracting the day, month, and year components and then rearranging them into the desired format using a lookup for month names.
Here's how the algorithm would work step-by-step:
def reformat_date(date):
date_components = date.split()
day = date_components[0]
month = date_components[1]
year = date_components[2]
month_map = {
'Jan': '01', 'Feb': '02', 'Mar': '03',
'Apr': '04', 'May': '05', 'Jun': '06',
'Jul': '07', 'Aug': '08', 'Sep': '09',
'Oct': '10', 'Nov': '11', 'Dec': '12'
}
# Convert the month from name to numerical representation
month_number = month_map[month]
day_number = ''.join(filter(str.isdigit, day))
# Add leading zero if the day is a single digit
if len(day_number) == 1:
day_number = '0' + day_number
# Construct the reformatted date string
reformatted_date = year + '-' + month_number + '-' + day_number
return reformatted_date
Case | How to Handle |
---|---|
Null or empty input string | Return an empty string or throw an IllegalArgumentException, depending on desired behavior. |
Invalid Day format (e.g., '1th', '32nd') | Implement validation for correct day suffixes and valid day numbers, potentially throwing an exception. |
Invalid Month format (e.g., 'Jann', 'Foo') | Use a map or a fixed array to validate the month string against a predefined set of valid month names, throwing an exception otherwise. |
Invalid Year format (e.g., '1899', '2101', 'abc') | Check if the year is within the range [1900, 2100] and is a valid integer, throwing an exception if not. |
Date string with extra spaces or leading/trailing spaces | Trim the input string and split the string into its parts based on a single space delimiter. |
Incorrect number of components in the date string (more or less than three parts) | Check the number of parts after splitting the input string and return an appropriate error or handle the exception gracefully. |
Leap year day handling (Feb 29th in non-leap years) | While this problem does not ask to validate if the date is a *real* date, this would be part of a production validation implementation to handle edge cases like invalid dates. |
Day having single digit number (e.g., 1st). | Pad the day with a leading zero if it's a single digit number after removing the suffix. |