Count Months Starting on Sunday
Updated
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. Otherwise it has 28 days. 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) → intExamples
Example 1
startYear = 2000endYear = 2024return = 43Step forward one month at a time from the Saturday given above. Exactly 43 months from January 2000 through December 2024 begin on a Sunday, so that is the answer.
Example 2
startYear = 2000endYear = 2000return = 1Within 2000, October is the only month whose first day is Sunday.
Example 3
startYear = 2000endYear = 2004return = 8Check the first day of every month in these five years. 8 of them fall on a Sunday, so that is the answer. Both 2000 and 2004 are leap years, so their February has the extra day.
Constraints
2000 <= startYear <= endYear <= 9999.- Both endpoints are included.
- Work out every weekday from the calendar rules above. Do not use a date or calendar library.
More Walmart Phone Screen questions
- Longest Even-Length WordPHONE SCREEN · Seen Aug 2026
- Range ModulePHONE SCREEN · Seen Aug 2026
- Meeting Rooms IIPHONE SCREEN · Seen Aug 2026
- Sort Real Number StringsPHONE SCREEN · Seen Jan 2026
- Check a Password Against a Supplied PolicyPHONE SCREEN · Seen Jun 2025
- Convert a Restricted Function to an Arrow FunctionPHONE SCREEN · Seen Jun 2025
- Repair One Missing Function Punctuation CharacterPHONE SCREEN · Seen Jun 2025
- Can the Singer Perform the Song?PHONE SCREEN · Seen Mar 2025
- Rooms With Two Other Entrances and a Treasure SuccessorPHONE SCREEN · Seen Mar 2025
- First Dictionary Word from Note LettersPHONE SCREEN · Seen Aug 2022
Similar questions at other companies
- Parking Fee CalculatorBreeze · PHONE SCREEN
- Automation Pipeline, Part 1: Single WorkerPlaid · PHONE SCREENPlaid interview questions
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Fair Coin from a Biased CoinAnthropic · PHONE SCREENAnthropic interview questions
- Three Points in a Semicircle: Monte Carlo EstimateSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Apply Affine Array TransformationsByteDance · PHONE SCREENByteDance interview questions
- Exponential Backoff DelaysWonderful · PHONE SCREEN
- Add Two Integers Given as StringsJane Street · PHONE SCREENJane Street interview questions
- Fizz and Buzz Words From 1 to nDocuSign · PHONE SCREENDocuSign interview questions
- Recover Document Hierarchy from IndentationReducto · PHONE SCREEN