FastPrepCount Months Starting on Sunday

Count Months Starting on Sunday

Walmart logoWalmart● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Given two years startYear and endYear, count the months in that inclusive range whose first day is a Sunday.

Use the Gregorian calendar. January 1, 2000 was a Saturday. A leap year is divisible by 400, or it is divisible by 4 but not by 100. February has 29 days in a leap year and 28 otherwise; April, June, September, and November have 30 days; every other month has 31 days.

Calculate the weekdays directly. Do not use a date or calendar library.

Function

countSundayMonthStarts(startYear: int, endYear: int) → int

Examples

Example 1

startYear = 2000endYear = 2024return = 43

Advancing one month at a time from the known Saturday anchor shows that exactly 43 month starts from January 2000 through December 2024 fall on Sunday.

Example 2

startYear = 2000endYear = 2000return = 1

Within 2000, October is the only month whose first day is Sunday.

Example 3

startYear = 2000endYear = 2004return = 8

Counting each inclusive month start across these five years gives 8. Both 2000 and 2004 use the leap-year February length.

Constraints

  • 2000 <= startYear <= endYear <= 9999.
  • Both endpoints are included.
  • Weekday values must be derived from the supplied calendar rules without a date or calendar library.

More Walmart problems

See Walmart hiring insights
public int countSundayMonthStarts(int startYear, int endYear) {
    // Write your code here.
}
startYear2000
endYear2024
expected43
Checking account…