Watch 10 video solutions for Day of the Week, a easy level problem involving Math. This walkthrough by Codebix has 6,657 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.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 | 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 |