Skip to content

Volume ITechnology

4.0.2 — First Warm-Ups: Even or Odd, Leap Year, Largest of Three, FizzBuzz ​

Classic · Easy · asked as the first two minutes of a round

These four are short. Each one has one small detail that people often get wrong. That detail is what the interviewer looks for.

1. Even or odd ​

The problem ​

Print whether a whole number is even or odd.

The solution ​

python
def even_or_odd(n: int) -> str:
    return "even" if n % 2 == 0 else "odd"

print(even_or_odd(10))   # → even
print(even_or_odd(-7))   # → odd

A number is even when 2 divides it with nothing left over. n % 2 is that leftover.

Common mistake. People write n % 2 == 1 for odd. In Python that works for negatives, because -7 % 2 is 1. In Java, C and JavaScript, -7 % 2 is -1, so the check says "not odd" and the code breaks. Always test == 0 and treat everything else as odd. Then it works in every language.

The bit version. The last bit of a binary number is the "ones" place. It is 1 for odd numbers and 0 for even ones. So n & 1 is 1 exactly when n is odd. & is bitwise AND: it keeps a bit only where both numbers have a 1. n & 1 keeps just the last bit. Interviewers sometimes ask "do it without %", and this is the answer.

python
print(13 & 1)   # → 1   (13 is 1101 in binary, so odd)
print(10 & 1)   # → 0   (10 is 1010 in binary, so even)

2. Leap year ​

The problem ​

Given a year, say whether it is a leap year.

Why the rule has three parts ​

The Earth takes about 365.2422 days to go round the Sun. A calendar of 365 days drifts by almost a quarter of a day each year. Adding one day every 4 years fixes most of that. But it over-corrects slightly, because 0.25 is a bit more than 0.2422.

So in 1582 Pope Gregory XIII's calendar added two more rules. Century years are not leap years, which removes 3 days every 400 years. But years divisible by 400 are leap years. This is the Gregorian calendar we use today. Its average year is 365.2425 days, which is very close to the real value.

So the rule is:

  1. Divisible by 400: leap year.
  2. Otherwise, divisible by 100: not a leap year.
  3. Otherwise, divisible by 4: leap year.
  4. Otherwise: not a leap year.

The solution ​

python
def is_leap(year: int) -> bool:
    return year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)

for y in (2024, 1900, 2000, 2023):
    print(y, is_leap(y))
# → 2024 True
# → 1900 False
# → 2000 True
# → 2023 False

Common mistake. Most wrong answers check only year % 4 == 0. The interviewer will then ask about 1900. 1900 is divisible by 4 but it was not a leap year. 2000 was a leap year because it is divisible by 400. These two years are the test cases to say out loud. Python's own calendar.isleap(year) does exactly this check.

3. Largest of three numbers ​

The problem ​

Return the largest of a, b and c.

The solution ​

python
def largest(a: int, b: int, c: int) -> int:
    best = a            # (1)
    if b > best:
        best = b        # (2)
    if c > best:
        best = c
    return best

print(largest(4, 9, 2))   # → 9
print(largest(7, 7, 3))   # → 7

(1) Assume the first is the largest.

(2) Each other number replaces it only if it is bigger.

In real Python you write max(a, b, c). The interviewer bans max because they want to see the "keep the best so far" idea. It is the same idea you use to find the largest element of a whole array in 4.0.15.

Common mistake. A common wrong answer forgets about ties.

python
def largest_buggy(a, b, c):
    if a > b and a > c:
        return a
    elif b > a and b > c:
        return b
    else:
        return c

print(largest_buggy(7, 7, 3))   # → 3

With (7, 7, 3), the test a > b is false, because 7 is not greater than 7. The test b > a is false for the same reason. So the code falls through to c and returns 3, which is the smallest. Using >= fixes it. The running-best version never had the problem.

4. FizzBuzz ​

The problem ​

Print the numbers 1 to 15. For multiples of 3 print Fizz instead. For multiples of 5 print Buzz. For multiples of both print FizzBuzz.

It started as a children's counting game in Britain. In 2007 programmer Imran Ghory wrote that many job candidates with good CVs could not solve it. Since then it has been used everywhere as a quick check that someone can actually write a loop.

The solution ​

python
def fizzbuzz(n: int) -> list[str]:
    out = []
    for i in range(1, n + 1):
        if i % 15 == 0:          # (1)
            out.append("FizzBuzz")
        elif i % 3 == 0:
            out.append("Fizz")
        elif i % 5 == 0:
            out.append("Buzz")
        else:
            out.append(str(i))
    return out

print(" ".join(fizzbuzz(15)))
# → 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz

(1) Check "both" first. A number divisible by both 3 and 5 is divisible by 15, because 3 and 5 share no common factor.

Common mistake. If you check i % 3 first, then 15 matches it, prints Fizz, and the elif chain stops. You never reach FizzBuzz. Order matters. The most specific case goes first.

The version that scales. An interviewer may ask, "Now add 7 for Bazz." Writing out every combination would need eight branches. Building the word piece by piece needs one line per rule.

python
def fizzbuzz_rules(n: int, rules: list[tuple[int, str]]) -> list[str]:
    out = []
    for i in range(1, n + 1):
        word = "".join(text for d, text in rules if i % d == 0)
        out.append(word or str(i))      # empty word means no rule matched
    return out

print(fizzbuzz_rules(21, [(3, "Fizz"), (5, "Buzz"), (7, "Bazz")])[-1])   # → FizzBazz

21 is divisible by 3 and by 7, so it collects Fizz and then Bazz.

Complexity ​

Even or odd, leap year and largest of three are O(1). FizzBuzz is O(n) for n numbers.

What the interviewer will push on ​

These are the questions that usually come right after you solve it. Each one has a short, plain answer you can say out loud.

"Can you check odd or even without using %?" Yes. Use n & 1. The last binary digit of an odd number is always 1, and of an even number is always 0. n & 1 keeps only that last digit.

"Is 1900 a leap year?" No. 1900 divides by 4, but it also divides by 100 and not by 400. Century years are leap years only when they divide by 400. So 2000 was a leap year, and 1900 was not.

"Now add a rule: multiples of 7 print Bazz." Do not add more if branches for every mix of rules. Keep a list of rules, like [(3, "Fizz"), (5, "Buzz"), (7, "Bazz")]. For each number, join the words of every rule that matches. If no rule matches, print the number.

One thing to volunteer: "I check odd numbers with n % 2 != 0, not n % 2 == 1. In Java, -7 % 2 is -1, so == 1 would call -7 even. Comparing with 0 works in every language."

Next: 4.0.3 Working With Digits. Counting, summing and reversing digits, all with the digit loop from 4.0.0.

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Volume I — Technology
Part 0 — Retention & Notation
Part 1 — Foundations of Computation
Part 2 — Operating Systems
2.1 The Kernel
2.2 Processes
2.3 Threads & Scheduling
2.4 Concurrency Under the Hood
2.5 Memory Management
2.6 File Systems
2.7 Input/Output
2.8 Linux for Engineers
2.9 Windows & macOS in Practice
Part 3 — Languages, Compilers & Runtimes
3.6 JavaScript & V8
3.7 TypeScript
3.8 Node.js Internals
3.13 Python & CPython
3.14 FastAPI & Production Python
Part 4 — Data Structures & Algorithms
4.0 Classic Problems — Before NeetCode
4.4 Arrays & Hashing — Problems
4.5 Two Pointers
4.6 Sliding Window
4.8 Stack — Problems
4.9 Linked List — Problems
4.11 Binary Search — Problems
4.13 Trees
4.14 Tree — Problems
4.15 Trie — Problems
4.17 Heap — Problems
4.18 Backtracking — Problems
4.19 Graphs
4.20 Graph — Problems
4.21 Advanced Graph — Problems
4.23 1-D DP — Problems
4.24 2-D DP — Problems
4.26 Greedy — Problems
4.27 Interval — Problems
4.28 Math & Geometry — Problems
4.30 Bit Manipulation — Problems
Part 5 — Networking & the Web
5.3 IP
5.4 Transport
5.6 HTTP
Part 6 — Frontend Engineering
6.1 Browser Internals
6.2 HTML & CSS Mastery
6.3 JavaScript in the Browser
6.4 React, from First Component to Full Shop
6.5 Frameworks & Rendering
6.8 UI Engineering & Patterns
Part 7 — Databases & Storage
7.2 SQL Mastery
7.3 Storage Engine Internals
7.4 Transactions
7.5 NoSQL Taxonomy
7.8 Analytics & Big Data
Part 8 — Security, Identity & Cryptography
8.2 Cryptography
8.3 Protocols of Trust
8.4 Identity: AuthN & AuthZ
8.5 Application Security
8.6 Infrastructure Security
Part 9 — Software Design (LLD)
9.2 OOP Foundations
9.3 Design Principles
9.4 Design Patterns
9.5 Concurrency
9.6 API Design
9.7 LLD Interview Machines
9.9 Node/Express Production Playbook
Part 10 — Distributed Systems & HLD
10.7 Consistency & Consensus
10.8 Messaging & Microservices
10.14 Caching
Building blocks from the design seat
Part 11 — System Design Case Studies
Part 12 — AI & ML Engineering
12.1 What AI Engineering Is
12.2 The Math You Need
12.3 Classical Machine Learning
12.4 Data for ML
12.5 Deep Learning
12.6 Embeddings & Vector Search
12.7 Transformers & LLMs
12.8 Building with LLMs
12.9 Retrieval-Augmented Generation
12.10 Agents
12.11 Adapting Models
12.12 Production AI
12.13 Multimodal AI
12.14 AI Security & Governance
12.15 Case Studies & Capstone
Part 13 — Cloud, DevOps & Infrastructure
13.11 Git, from First Commit to Team Workflow
Part 14 — Containers: Docker & Kubernetes
14.1 Foundations
14.2 Docker
14.3 Kubernetes
14.4 Kubernetes on Azure
Part 15 — The Craft & Career
Nothing picked yet