Day of the Week - Problem

Ever wondered what day of the week you were born on? Or what day Christmas will fall on next year? This problem asks you to determine the day of the week for any given date!

You are given a date as three integers: day, month, and year. Your task is to return the corresponding day of the week as a string from the set: {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.

Key reference point: January 1, 1971 was a Friday. You can use this as your starting point to calculate any other date!

Example: If given day = 31, month = 12, year = 2019, you should return "Tuesday" because December 31, 2019 was a Tuesday.

Input & Output

example_1.py โ€” Basic Date
$ Input: day = 31, month = 12, year = 2019
โ€บ Output: "Tuesday"
๐Ÿ’ก Note: December 31, 2019 was a Tuesday. We can verify this by counting days from January 1, 1971 (Friday) or using the mathematical formula.
example_2.py โ€” Leap Year
$ Input: day = 29, month = 2, year = 2020
โ€บ Output: "Saturday"
๐Ÿ’ก Note: February 29, 2020 was a Saturday. This tests our leap year handling since 2020 is divisible by 4.
example_3.py โ€” Reference Date
$ Input: day = 1, month = 1, year = 1971
โ€บ Output: "Friday"
๐Ÿ’ก Note: This is our reference point given in the problem statement. January 1, 1971 was a Friday.

Constraints

  • 1 โ‰ค day โ‰ค 31
  • 1 โ‰ค month โ‰ค 12
  • 1971 โ‰ค year โ‰ค 2100
  • The given date is guaranteed to be valid
  • No need to validate input dates

Visualization

Tap to expand
Jan 1, 1971FridayReference PointMathematical JumpDay-by-day CountAny TargetDec 31, 2019Tuesday17,896 days later%7Days cycle every 7Key Insights๐Ÿ”„ Days repeat in cycles of 7๐Ÿ“… Leap years add extra day every 4 years๐Ÿงฎ Mathematical formulas can skip the countingExample: Dec 31, 2019Brute Force: 1971โ†’2018 (48 years) + Janโ†’Nov 2019 (334 days) + 30 more days = 17,896 totalMathematical: Apply formula directly โ†’ h = 2, adjusted = (2+5)%7 = 0 โ†’ TuesdayBoth methods: 17,896 % 7 = 2 days after Friday โ†’ Sunday(0), Monday(1), Tuesday(2) โœ“Result: Tuesday
Understanding the Visualization
1
Know Your Reference
January 1, 1971 was a Friday - this is your anchor point
2
Count or Calculate
Either count every day or use math to jump directly to the answer
3
Handle Patterns
Days repeat every 7, leap years add extra days, months have different lengths
4
Get Final Answer
Use modulo 7 to convert total days into day of the week
Key Takeaway
๐ŸŽฏ Key Insight: Since days repeat every 7, we only need to find how many days have passed since our reference date (modulo 7). We can either count every day or use mathematical formulas to jump directly to the answer!
Asked in
Microsoft 25 Amazon 18 Google 15 Apple 12
32.4K Views
Medium Frequency
~15 min Avg. Time
892 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