Skip to content
spot_the_ai_bug

Null & undefined bugs: interview questions

Null-handling bugs assume a value exists when it may not: a missing map key, an empty Optional, a first-time visitor's empty storage, or NULL in SQL's three-valued logic.

How to spot it in code review

  • For every lookup, ask "what if it's not there?"
  • Treat ! and .get() as claims that need proof.
  • In SQL, remember NULL = NULL is not TRUE.
  • Check first-run and deleted-record paths explicitly.

Why AI-generated code gets this wrong

Models optimize for code that compiles, and non-null assertions are the quickest way to silence a type error.

Null & undefined bugs: 3 practice questions

Q1TypeScriptmedium

Reading preferences from localStorage

loadTheme() runs on page load. It works for returning visitors. What happens on someone's first visit?

interface Prefs {  theme: "light" | "dark";} export function loadTheme(): Prefs["theme"] {  const prefs: Prefs = JSON.parse(localStorage.getItem("prefs")!);  return prefs.theme;}
Show answer

line 6 · Null / undefined handling

On a first visit getItem returns null. The ! hides that from TypeScript, JSON.parse(null) returns null, and prefs.theme throws a TypeError that breaks page load.

Fix
  const raw = localStorage.getItem("prefs");  const prefs: Partial<Prefs> = raw ? JSON.parse(raw) : {};  return prefs.theme ?? "dark";

Interview follow-up: Also consider corrupted JSON: wrap JSON.parse in try/catch for data you don't control.

Q2Javamedium

Calling Optional.get()

greetingFor() builds the email greeting for the password-reset flow. What happens for an email that isn't registered?

public String greetingFor(String email) {    Optional<User> user = userRepository.findByEmail(email);    return "Hi " + user.get().getFirstName() + ",";}
Show answer

line 3 · Null / undefined handling

findByEmail returns Optional.empty() for unknown emails, and get() on an empty Optional throws NoSuchElementException. The reset endpoint returns a 500, which also tells attackers which emails exist.

Fix
    return userRepository.findByEmail(email)            .map(u -> "Hi " + u.getFirstName() + ",")            .orElse("Hi there,");

Interview follow-up: Optional exists to force you to handle absence; get() without isPresent() defeats the point.

Q3SQLeasy

Filtering with = NULL

This should list users who haven't verified their email (verified_at is NULL). Why does it always return zero rows?

SELECT id, emailFROM usersWHERE verified_at = NULLORDER BY created_at;
Show answer

line 3 · Null / undefined handling

Any comparison with NULL using = yields NULL (unknown), never TRUE, so no row passes the filter. NULL checks need IS NULL.

Fix
WHERE verified_at IS NULL

Interview follow-up: Related traps: NOT IN with a NULL in the list, and COUNT(column) skipping NULLs.

Every day, everyone gets the same pull request written by AI. It looks clean. It has comments. It has exactly one real bug.

  1. 1

    Find the line

    Tap the buggy line, then hit Submit. You get 3 attempts.

  2. 2

    Read the hints

    A miss within 2 lines is 🔥 Warm. Anything further is ❄️ Cold.

  3. 3

    Name the bug

    Found it? Bonus round: pick the bug type from 4 options. It doesn't affect your win, just your bragging rights.

// your share card

found in 3/3

🟥 cold miss · 🟨 warm miss · 🟩 found · ⬛ unused

Keyboard: ↑ ↓ to move between lines, Enter to submit. A new bug drops every day at midnight IST.

0
Played
0
Win %
0
Streak
0
Max

Attempts distribution