Day of the Week - Problem

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"}.

Note: January 1, 1971 was a Friday.

Input & Output

Example 1 — Basic Case
$ Input: day = 31, month = 12, year = 1999
Output: Friday
💡 Note: December 31, 1999 was a Friday. We can verify this by counting days from January 1, 1971 or using Zeller's formula.
Example 2 — Leap Year
$ Input: day = 29, month = 2, year = 2000
Output: Tuesday
💡 Note: February 29, 2000 was a Tuesday. Year 2000 is a leap year (divisible by 400).
Example 3 — Reference Date
$ Input: day = 1, month = 1, year = 1971
Output: Friday
💡 Note: January 1, 1971 was a Friday as given in the problem statement.

Constraints

  • 1 ≤ day ≤ 31
  • 1 ≤ month ≤ 12
  • 1971 ≤ year ≤ 2100
  • The given date is guaranteed to be valid

Visualization

Tap to expand
Day of the Week - Optimal Solution INPUT December 1999 Su Mo Tu We Th Fr Sa 1 2 3 4 26 27 28 29 30 31 Input Values: day=31 month=12 year=1999 Reference: Jan 1, 1971 = Friday (Day index: 5) ALGORITHM STEPS 1 Count Total Days From Jan 1, 1971 to target 2 Add Years Contribution 365 days + leap years 1971-1998: 28 years = 28*365 + 7 leaps = 10227 3 Add Months Days Jan-Nov 1999 = 334 days [31,28,31,30,31,30,31,31,30,31,30] 4 Calculate Day Index (totalDays + 4) % 7 total = 10227 + 334 + 31 = 10592 days (10592 + 4) % 7 = 5 FINAL RESULT Day Index Mapping: 0 --> Sunday 1 --> Monday 2 --> Tuesday 3 --> Wednesday 4 --> Thursday 5 --> Friday <-- Output: "Friday" Dec 31, 1999 = Friday OK Key Insight: Since Jan 1, 1971 is Friday (index 5), we count total days from that date and use modulo 7. Formula: dayIndex = (totalDays + 4) % 7 where 4 shifts Friday to index 5 in 0-indexed week. Handle leap years: divisible by 4, except centuries unless divisible by 400. TutorialsPoint - Day of the Week | Optimal Solution - O(1) Time, O(1) Space
Asked in
Amazon 25 Microsoft 20 Google 15
32.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen