Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Return `(y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)`. The 400-rule overrides the 100-exception.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you can translate a spec with an exception-to-an-exception into correct boolean logic.
Where candidates slip
Only testing divisibility by 4 and mishandling century years.
How to stand out
Call out 1900 (false) and 2000 (true) yourself as the boundary cases that validate the rule.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Boolean rule
1Hint 1Explore hintHide hint
Start with the simple rule (divisible by 4), then layer the exceptions.
2Hint 2Explore hintHide hint
Centuries (÷100) are skipped — unless they're also ÷400.
3Hint 3Explore hintHide hint
Encode it as one OR of two ANDs; test it on 1900 and 2000.
A · Core idea
The rule has an exception inside an exception: a year is a leap year if it is divisible by 4, except centuries (divisible by 100), unless the century is also divisible by 400. The cleanest encoding is one boolean expression: (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0).
Why this exact shape. The first clause captures 'divisible by 4 but not a plain century'. The second clause, OR-ed in, re-admits the special centuries (2000, 2400) that are divisible by 400. Because the 400-rule is its own OR term, it cleanly overrides the 100-exception without nested conditionals.
The classic gotcha: 1900 vs 2000. 1900 is divisible by 100 but not 400, so it is not a leap year — many naive solutions that only check % 4 get this wrong. 2000 is divisible by 400, so it is. These two years are the test the interviewer is really running.
Translating an English spec with nested exceptions into one OR-of-ANDs is the transferable skill here — it shows up again in date math, validation rules, and permission checks.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
y = 1900, then y = 20001900 % 4
0 → divisible by 4
candidate1900 → false, 2000 → trueC · Code with teaching comments
D · Why it works and complexityO(1) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
Getting the precedence right between the 100 and 400 rules is the whole test — 1900 vs 2000 is the classic gotcha.
ComplexityO(1) time, O(1) space — three modulo checks at most — constant.
E · Edge cases and pitfalls5y = 2024 — Divisible by 4, not a century → leap year (true).Explore edge casesHide edge cases
Edge cases handled
y = 2024Divisible by 4, not a century → leap year (true).y = 2100Century not divisible by 400 → false.y = 2400Century divisible by 400 → true.
Pitfalls
- Checking only `y % 4 == 0`, which wrongly marks 1900 as a leap year.
- Getting the precedence of the 100 and 400 rules backwards.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic boolean isLeap(int y) { return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0); }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Only checking divisibility by 4
It marks every century (1700, 1800, 1900) as a leap year, which is wrong unless they're also divisible by 400.
Explore the code comparisonHide the code comparison
Only checking divisibility by 4
It marks every century (1700, 1800, 1900) as a leap year, which is wrong unless they're also divisible by 400.
Don't write this
return y % 4 == 0; // 1900 -> true (WRONG)Write this instead
return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);Revise
Carry the pattern into the interview
Pattern to remember
Compound divisibility rule
Code pattern to memorize
(y%4==0 && y%100!=0) || (y%400==0)Rules to remember
- 1Div by 4 and not by 100 → leap.
- 2Div by 400 → always leap.
- 31900 false, 2000 true.
Use this pattern when
- Rules with exceptions-to-exceptions
- Encoding a spec as one boolean expression
Interview takeaway
When rules have exceptions-to-exceptions, encode them as one OR of ANDs.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Compound divisibility rule. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Day of the WeekComing soonDate math that depends on knowing leap years to count February days.