Quick Overview

Count months beginning on Sunday across an inclusive Gregorian year range without a calendar library, using the 2000 weekday anchor and correct forward, backward, and leap-year arithmetic.

Count Months Whose First Day Is Sunday

Company: Walmart Labs

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem Given inclusive start and end years, count how many months in that range begin on a Sunday. Use the proleptic Gregorian calendar and do not use a built-in date or calendar library. You may use the anchor that January 1, 2000 was a Saturday. ### Function Contract Implement `count_sunday_month_starts(start_year, end_year) -> int`. ### Constraints - `1900 <= start_year <= end_year <= 9999`. - A leap year is divisible by 400, or divisible by 4 but not by 100. - Count January through December in both boundary years. - Weekday arithmetic must work for ranges before as well as after 2000. ### Examples - `start_year = 2000`, `end_year = 2024` returns `43`. - The year 2000 is a leap year; 1900 is not. ```hint Track the first weekday Once the weekday of a month's first day is known, add that month's length modulo seven to reach the next month. ``` ```hint Reach the starting year from the anchor Move year by year forward or backward from 2000 using 365 or 366 days modulo seven. ``` ### Edge Cases - Century leap-year rules affect 1900, 2000, and 2100 differently. - A one-year range still includes all twelve months. - Do not assume the start year is at or after 2000.

Overview: Count months beginning on Sunday across an inclusive Gregorian year range without a calendar library, using the 2000 weekday anchor and correct forward, backward, and leap-year arithmetic.

Given inclusive start and end years, count how many months from January of the start year through December of the end year begin on a Sunday. Use the proleptic Gregorian calendar and do not use a built-in date or calendar library. January 1, 2000 was a Saturday. A leap year is divisible by 400, or divisible by 4 but not by 100. Weekday arithmetic must work before and after 2000.

Constraints

  • 1900 <= start_year <= end_year <= 9999.
  • The range includes January through December in both boundary years.
  • A leap year is divisible by 400, or divisible by 4 but not by 100.
  • Use the proleptic Gregorian calendar without a built-in date or calendar library.
  • January 1, 2000 was a Saturday.

Examples

Input: (2000, 2024)

Expected Output: 43

Explanation: The inclusive 25-year range contains 43 Sunday month starts.

Input: (2000, 2000)

Expected Output: 1

Explanation: In leap year 2000, October begins on Sunday.

Hints

  1. Advance a month's first weekday by its length modulo seven.
  2. Reach the starting year by moving forward or backward from the 2000 anchor.

Loading coding console...

Show the approach

Approach

Represent Sunday through Saturday as 0 through 6, so the anchor weekday is 6. Move from January 1, 2000 to January 1 of the start year by adding or subtracting each intervening year's length modulo seven. Then visit every month in the inclusive range, count weekday zero, and advance by that month's Gregorian length.

Time complexity:
O(abs(start_year - 2000) + end_year - start_year + 1)
Space complexity:
O(1)