Decide whether a year is a leap year with the full Gregorian rule: divisible by 4, except centuries, unless divisible by 400. Python conditions practice.
The problem
Return True if year is a leap year in the Gregorian calendar:
- divisible by 4, except
- years divisible by 100 are not, unless
- they are also divisible by 400.
Examples
Example 1
Input
is_leap(2024)
Expected output
True
Example 2
Input
is_leap(1900)
Expected output
False
Example 3
Input
is_leap(2000)
Expected output
True
+ 3 hidden tests on Submit.
Edge cases to ask about
- Century years (1900, 2100)
- Multiples of 400
Hints
0/3How an interviewer scores this
0/9Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.
Complexity Lab
What does this cost as n grows?
Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and read why.
Pick both to reveal the answer.
From brute force to optimal
The progression an interviewer wants to hear, one step at a time.
| Approach | Time | Space | Idea |
|---|---|---|---|
| One boolean expression | O(1) | O(1) |
Walkthrough of the optimal approach (try it yourself first)
year % 4 == 0 and (year % 100 != 0 or year % 400 == 0). 1900 is divisible by 4 and by 100 but not by 400, so it is not a leap year; 2000 is.
calendar.isleap exists — know it, but be able to write the rule.
Complexity: O(1) time, O(1) space. Three modulo checks, regardless of the year.
Reveal the reference solution
def is_leap(year): return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
Follow-ups interviewers ask
- Count the leap years between two years in O(1).
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Check If a Year Is a Leap Year in Python?
The optimal solution runs in O(1) time and O(1) auxiliary space. Three modulo checks, regardless of the year.
What follow-up questions do interviewers ask about Check If a Year Is a Leap Year?
Count the leap years between two years in O(1).
