Day of the Week - Solution & Explanation
Problem Statement
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:
- The given dates are valid dates between the years
1971and2100.
Approach Overview
Problem Overview: Given a specific date (day, month, year), return the corresponding weekday such as Monday or Sunday. The challenge is converting a calendar date into a weekday index using either built-in date utilities or pure mathematical formulas.
Approach 1: Using Built-in Date Libraries (O(1) time, O(1) space)
The simplest solution relies on the standard date libraries available in most languages. You construct a date object using the provided day, month, and year, then call a function that returns the weekday index. Most libraries internally handle leap years, calendar transitions, and weekday calculation using optimized algorithms. After retrieving the weekday number, map it to the corresponding string (Sunday through Saturday). This approach is extremely reliable and concise, making it ideal in production code where correctness and readability matter more than implementing calendar math manually.
Approach 2: Implementing Zeller's Congruence (O(1) time, O(1) space)
Zeller's Congruence is a mathematical formula that directly computes the weekday for any Gregorian calendar date. The formula transforms the date into a modular arithmetic expression that returns an integer representing the day of the week. The key idea is adjusting months so that March becomes the first month of the year in the calculation, and January and February are treated as months 13 and 14 of the previous year. After computing the congruence value, apply a modulo operation to obtain a number between 0 and 6, then map it to the weekday name. This approach demonstrates deeper understanding of math and modular arithmetic, which interviewers often appreciate.
Zeller's formula works because it accounts for leap years and century corrections using integer division and modular arithmetic. The computation involves extracting the year of the century and the zero-based century, then combining them with the day and month offsets. Although the formula looks dense, it runs in constant time and requires only a few arithmetic operations.
Recommended for interviews: Implementing Zeller's Congruence usually makes a stronger impression because it shows you understand the underlying mathematical reasoning behind calendar calculations. The built-in library solution is perfectly valid and often expected in real-world systems, but the mathematical approach demonstrates algorithmic thinking and familiarity with classic date algorithms.
Approach 1: Using Built-in Libraries
Most programming languages have built-in libraries or modules that can handle date and time calculations. These libraries can directly provide functionalities to compute the day of the week for a given date, ensuring efficient and accurate results without manually implementing complex algorithms.
This solution uses Python's datetime library. A date object is created using the provided year, month, and day. The strftime method with '%A' format code is used to get the full weekday name.
Complexity
Time Complexity: O(1), Space Complexity: O(1)
Approach 2: Implementing Zeller's Congruence
Zeller's Congruence is a well-known algorithm that provides a formula to determine the day of the week for any given date. It accounts for leap years and the calendar adjustments made historically.
This Python solution implements Zeller's Congruence. The formula computes an integer that corresponds to a day of the week, which is mapped to a name in the days array.
Complexity
Time Complexity: O(1), Space Complexity: O(1)
Approach 3: Zeller's Congruence
We can use Zeller's Congruence to calculate the day of the week. Zeller's Congruence is as follows:
$
w = (\left \lfloor \frac{c}{4} \right \rfloor - 2c + y + \left \lfloor \frac{y}{4} \right \rfloor + \left \lfloor \frac{13(m+1)}{5} \right \rfloor + d - 1) bmod 7
Where:
w: Day of the week (starting from Sunday)c: First two digits of the yeary: Last two digits of the yearm: Month (the range of m is from 3 to 14, that is, in Zeller's Congruence, January and February of a certain year are considered as the 13th and 14th month of the previous year. For example, January 1, 2003 is considered as the 1st day of the 13th month of 2002)d: Dayāā: Floor function (round down)mod: Modulo operation
The time complexity is O(1), and the space complexity is O(1)$.
Code
Python
Java
C++
Go
TypeScript
Approach 4: Default Approach
Try this approach in the editor āComplexity Comparison
| Approach | Complexity |
|---|---|
| Using Built-in Libraries | Time Complexity: O(1), Space Complexity: O(1) |
| Implementing Zeller's Congruence | Time Complexity: O(1), Space Complexity: O(1) |
| Zeller's Congruence | ā |
| Default Approach | ā |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Built-in Date Libraries | O(1) | O(1) | Best for production code where readability and reliability matter |
| Zeller's Congruence Formula | O(1) | O(1) | Preferred in interviews to demonstrate mathematical reasoning |
Video Solution
leetcode 1185 Day of the Week ⢠Codebix ⢠6,800 views views
Watch 9 more video solutions āFrequently Asked Questions
Is Day of the Week easy or hard?
Day of the Week Python/Java solution
How to solve Day of the Week in O(1)?
What is the best approach for Day of the Week?
Is Day of the Week asked at Google/Amazon/Meta?
What data structure is used in Day of the Week?
What is the time complexity of Day of the Week?
Ready to solve this problem?
Practice Day of the Week with our built-in code editor and test cases.
Practice on FleetCodeTable of Contents
Practice this problem
Open in Editor