This is a premium problem. We're working on making it available for free soon.
Use these hints if you're stuck. Try solving on your own first.
Does February have 28 days or 29 days?
Think of Leap years.
Solutions for this premium problem will be available for free soon.
Browse Free ProblemsWatch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Problems like this are occasionally used in interviews to test attention to detail and understanding of edge cases such as leap years. While simple, they help assess basic logic and conditional handling in coding interviews.
A simple array or list of month lengths works best. It allows direct indexing using the month number and keeps the solution clean and efficient. Only February requires a conditional adjustment for leap years.
The optimal approach is to store the number of days for each month in an array and adjust February based on leap year rules. This avoids complex branching and allows constant-time lookup. A quick leap year check determines whether February has 28 or 29 days.
A year is a leap year if it is divisible by 4. However, years divisible by 100 are not leap years unless they are also divisible by 400. This rule ensures the calendar stays aligned with Earth's orbit.