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.In #1185 Day of the Week, the task is to determine the weekday for a given date. Since the Gregorian calendar follows predictable patterns, the key idea is to convert the given day, month, year into a numeric offset from a known reference date.
A common approach is to count the total number of days from a fixed base date (for example, 1971-01-01 used in the problem constraints). You add the days contributed by full years, handle leap years correctly, then add the days from the months and the given day. Once the total day offset is known, you can map it to the correct weekday using modulo arithmetic.
Another mathematical approach uses formulas such as Zeller’s Congruence to compute the weekday directly. Both approaches rely on careful handling of leap years and month offsets. Since the computation involves only arithmetic operations and fixed-size loops over months, the solution runs in constant time with O(1) space.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Count days from reference date | O(1) | O(1) |
| Zeller's Congruence / Mathematical formula | O(1) | O(1) |
NeetCode
Use these hints if you're stuck. Try solving on your own first.
Sum up the number of days for the years before the given year.
Handle the case of a leap year.
Find the number of days for each month of the given year.
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Problems like Day of the Week occasionally appear in interviews to test logical thinking and attention to detail. While not extremely common, they help evaluate a candidate’s ability to handle edge cases such as leap years and date calculations.
No complex data structure is required for this problem. Most solutions use simple arrays to store the number of days in each month and the list of weekday names. The main logic relies on arithmetic calculations rather than data structures.
The optimal approach is to convert the given date into the total number of days from a known reference date and then map the result to a weekday using modulo 7. This avoids heavy computation and works in constant time. Proper handling of leap years is essential for accuracy.
Leap years add an extra day to February, which shifts the weekday calculation for dates after February in that year. A year is typically a leap year if it is divisible by 4 but not by 100, unless it is also divisible by 400. Correctly applying this rule ensures accurate day counting.