LeetCode Problem Workspace

Day of the Week

Given a date, calculate the corresponding weekday using a math-based strategy, focusing on modular arithmetic.

category

1

Topics

code_blocks

5

Code langs

hub

3

Related

Practice Focus

Easy · Math-driven solution strategy

bolt

Answer-first summary

Given a date, calculate the corresponding weekday using a math-based strategy, focusing on modular arithmetic.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Math-driven solution strategy

Try AiBox Copilotarrow_forward

This problem requires calculating the weekday for a given date using a math-driven solution. The approach focuses on counting days and using modular arithmetic to identify the correct weekday. By summing up the number of days for the years before the given year, you can apply the necessary formula to find the result.

Problem Statement

Given a date represented by the day, month, and year, determine the corresponding day of the week. The input consists of three integers: day, month, and year.

Your task is to return the weekday of the specified date as one of the following strings: {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}. The dates are valid and range between the years 1971 and 2100.

Examples

Example 1

Input: day = 31, month = 8, year = 2019

Output: "Saturday"

Example details omitted.

Example 2

Input: day = 18, month = 7, year = 1999

Output: "Sunday"

Example details omitted.

Example 3

Input: day = 15, month = 8, year = 1993

Output: "Sunday"

Example details omitted.

Constraints

  • The given dates are valid dates between the years 1971 and 2100.

Solution Approach

Summing Days for Years Before the Given Year

Start by calculating the number of days that have passed since a reference date. You can sum up the number of days in the years before the given year using modular arithmetic to determine the offset for the weekday.

Utilize Zeller's Congruence

Zeller's Congruence is a popular algorithm for determining the day of the week for any given date. It uses the formula with year, month, and day, where the month is adjusted for January and February.

Account for Leap Years

Leap years affect the total number of days in a year. Ensure that your solution correctly handles leap years, which add one extra day to the year, especially for February.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

The time complexity depends on the final approach, with Zeller's Congruence having constant time complexity O(1). Space complexity can also be considered O(1), as the calculation does not require extra memory that scales with the input size.

What Interviewers Usually Probe

  • The candidate demonstrates a clear understanding of modular arithmetic and its application in date calculations.
  • The candidate correctly handles edge cases such as leap years and month boundaries.
  • The candidate exhibits a solid grasp of mathematical patterns involved in calculating days of the week.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to account for leap years, which may lead to incorrect weekday results for dates in February.
  • Incorrectly handling months for January and February when applying Zeller's Congruence, which adjusts the month by subtracting 1.
  • Overcomplicating the solution by introducing unnecessary loops or data structures when a constant time solution is possible.

Follow-up variants

  • Given a different date format, such as a string, adapt the solution to parse the input correctly before applying the same approach.
  • Solve the problem without using Zeller's Congruence by calculating the days in a more manual way.
  • Change the range of valid dates, for example from 1900 to 2050, and adjust the solution to handle that new range.

FAQ

How do I calculate the day of the week for a given date?

You can calculate the day of the week by using modular arithmetic or algorithms like Zeller's Congruence to count the number of days and adjust for leap years.

What is Zeller's Congruence and how does it help?

Zeller's Congruence is a formula used to calculate the day of the week for any given date. It uses the year, month, and day, with a specific adjustment for January and February.

Why do I need to account for leap years in this problem?

Leap years affect the number of days in a year, particularly in February. Failing to account for them could result in incorrect day-of-week calculations for certain dates.

How do I handle months in January and February in Zeller's Congruence?

In Zeller's Congruence, January and February are treated as the 13th and 14th months of the previous year, requiring adjustment to the year and month values.

Can this problem be solved in constant time?

Yes, using mathematical formulas like Zeller's Congruence, this problem can be solved in constant time O(1), as the algorithm only requires a few arithmetic operations.

terminal

Solution

Solution 1: Zeller's Congruence

We can use Zeller's Congruence to calculate the day of the week. Zeller's Congruence is as follows:

1
2
3
class Solution:
    def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
        return datetime.date(year, month, day).strftime('%A')

Solution 2

#### Python3

1
2
3
class Solution:
    def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
        return datetime.date(year, month, day).strftime('%A')
Day of the Week Solution: Math-driven solution strategy | LeetCode #1185 Easy