Skip to main content

Day of the Week - Solution & Explanation

EasyMath16 min readAsked at: Microsoft, Google, Zoho
Practice this problem

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 1971 and 2100.

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.

Code

Python

JavaScript

Java

C#

C++

C

Complexity

Time Complexity: O(1), Space Complexity: O(1)

Try this approach in the editor →

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.

Code

Python

JavaScript

Java

C#

C++

C

Complexity

Time Complexity: O(1), Space Complexity: O(1)

Try this approach in the editor →

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 year
  • y: Last two digits of the year
  • m: 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

Try this approach in the editor →

Approach 4: Default Approach

Code

Python

Java

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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

ApproachTimeSpaceWhen to Use
Built-in Date LibrariesO(1)O(1)Best for production code where readability and reliability matter
Zeller's Congruence FormulaO(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 is categorized as an Easy problem on LeetCode with an acceptance rate around 59%. The logic becomes straightforward when using built-in libraries, while implementing the mathematical formula adds a moderate conceptual challenge.
Day of the Week Python/Java solution
In Python, you can use the built-in datetime module and call weekday() or strftime to get the weekday name. In Java, LocalDate from java.time provides getDayOfWeek(). Both implementations compute the result in constant time.
How to solve Day of the Week in O(1)?
Use a direct calendar formula such as Zeller's Congruence. The formula converts the given day, month, and year into a weekday index using integer division and modular arithmetic. After computing the result modulo 7, map the number to the correct weekday string.
What is the best approach for Day of the Week?
The most practical approach uses built-in date libraries available in languages like Python, Java, and JavaScript. These libraries already implement optimized calendar algorithms and return the weekday in O(1) time. For interviews, implementing Zeller's Congruence is often preferred because it demonstrates understanding of calendar math and modular arithmetic.
Is Day of the Week asked at Google/Amazon/Meta?
Calendar and date calculation problems appear occasionally in interviews because they test attention to detail and mathematical reasoning. Variations of weekday calculation or date arithmetic have appeared in interviews at companies like Google and Amazon.
What data structure is used in Day of the Week?
No complex data structure is required. The solution relies mainly on arithmetic operations and a small array or list to map weekday indices (0–6) to names such as Sunday, Monday, and Tuesday.
What is the time complexity of Day of the Week?
Both common solutions run in O(1) time and O(1) space because the calculation involves only a fixed number of arithmetic operations or a single library call. No iteration or additional data structures are required.

Ready to solve this problem?

Practice Day of the Week with our built-in code editor and test cases.

Practice on FleetCode