Magicsheet logo

Day of the Week

Easy
12.5%
Updated 8/1/2025

Asked by 1 Company

Topics

Day of the Week

What is this problem about?

The Day of the Week interview question asks you to calculate the correct day name (e.g., "Monday") for a given date (day, month, year). You are typically given a reference date (like Jan 1, 1971, which was a Friday) and you need to determine the day for any date after that. This Day of the Week coding problem is a test of modular arithmetic and calendar logic.

Why is this asked in interviews?

Google uses this to check if you can handle multiple logical constraints (leap years, varying month lengths) and apply mathematical formulas. It tests your attention to detail regarding the Gregorian calendar rules and your ability to map a large range of days to a small set of outcomes (7 days) using the modulo operator.

Algorithmic pattern used

This utilizes the Math interview pattern.

  1. Reference Point: Pick a known day.
  2. Total Days Calculation: Count the number of days between the reference and the target date.
    • Sum days for every full year (365 or 366).
    • Sum days for every full month in the target year.
    • Add the days in the current month.
  3. Modulo: total_days % 7.
  4. Zeller's Congruence: Alternatively, you can use Zeller's mathematical formula, which calculates the day of the week directly from the date.

Example explanation

Target: Jan 3, 1971.

  1. Reference: Jan 1, 1971 was Friday (Index 5).
  2. Days passed: 2.
  3. (5 + 2) % 7 = 0.
  4. Result: Sunday. (Note: Indexing depends on whether you start your week on Sunday or Monday).

Common mistakes candidates make

  • Leap Year Logic: Forgetting that a year is a leap year if it's divisible by 4, but not by 100, unless it's also divisible by 400.
  • Off-by-one: Counting the target day incorrectly in the sum.
  • Month lengths: Using 30 days for every month instead of accounting for Feb, Apr, Jun, Sep, and Nov correctly.

Interview preparation tip

Memorize the "30 days hath September..." rhyme or store month lengths in an array. Handling leap years correctly is the most important part of this problem—always remember the "century rule" (100 and 400).

Similar Questions