Skip to the content

Lesson 156 of 296

Debugging and code review rounds

On this page

What these rounds look like

  • Debugging round: "This function has a bug. Users report wrong totals. Find and fix it." Sometimes in an unfamiliar codebase.
  • Code review round: "Here is a pull request. Review it as you would at work." You write or say comments.

Both test reading unfamiliar code quickly, finding the root cause rather than the symptom, and communicating clearly and kindly.

Reading unfamiliar code fast

  1. Start from the entry point: the function, route or component mentioned in the bug report.
  2. Read the names and types first, skip details. Build a rough map: "this loads orders, this calculates totals, this formats them".
  3. Follow the data: where does the wrong value come from?
  4. Read the tests, if there are any. They show what the author expected.
  5. Say your understanding out loud and check it with the interviewer.

The debugging method (Lesson 44)

  1. Reproduce the bug with a specific input.
  2. Narrow down where the value first goes wrong (logs, breakpoints, or tracing by hand).
  3. Find the root cause: ask "why?" until you reach the real reason. A symptom fix ("add a check to hide the NaN") is weaker than a root cause fix ("the quantity arrives as a string").
  4. Fix, then add a test that would have caught it.
  5. Check for the same bug elsewhere.

A practice debugging exercise

"Users say the basket total is sometimes wrong. Find the bugs."

function basketTotal(items, discountCode) {
  let total = 0;
  for (let i = 0; i <= items.length; i++) {
    total += items[i].price * items[i].qty;
  }
  if (discountCode = "SAVE10") {
    total = total * 0.9;
  }
  return total.toFixed(2);
}

Bugs to find (and say in this order: most serious first):

  1. i <= items.length reads one past the end: items[items.length] is undefined, so it crashes (off-by-one, Lesson 37).
  2. discountCode = "SAVE10" assigns instead of comparing, so every customer gets 10% off (Lesson 34).
  3. price as decimal pounds: floating point errors (Lesson 49). Use pence.
  4. If qty comes from a form, it may be a string; "2" * 5 happens to work, but + elsewhere would join strings. Validate and convert input.
  5. toFixed returns a string, so callers adding totals together will join strings.
  6. No handling of an empty or missing items, and no validation of negative quantities.

A fixed version:

TypeScript
type LineItem = { pricePence: number; qty: number };

function basketTotalPence(items: LineItem[], discountCode?: string): number {
  let total = 0;
  for (const item of items) {
    if (!Number.isInteger(item.qty) || item.qty < 0) {
      throw new ValidationError("quantity must be a whole number, 0 or more");
    }
    total += item.pricePence * item.qty;
  }
  if (discountCode === "SAVE10") {
    total = Math.round(total * 0.9);
  }
  return total;
}

Then add tests: empty basket, one item, with and without the code, an invalid quantity.

The code review round

Read the PR description, then the code, and look for problems in this order of importance:

PriorityWhat to checkExample comment
1. CorrectnessLogic errors, edge cases, off-by-one, null handling"If orders is empty, orders[0].date throws. Should we return early?"
2. SecurityUnvalidated input, SQL injection, missing permission checks, secrets in code, sensitive data in logs"This query builds SQL with string concatenation, so name could inject SQL. Could we use a parameterised query?"
3. Data and moneyFloating point money, time zones, missing transactions"Two writes here should succeed or fail together; can we wrap them in a transaction?"
4. TestsMissing tests for new behaviour and edge cases"Could we add a test for the expired-token case?"
5. PerformanceQueries in loops (N+1), unnecessary O(n²) work, missing pagination"This loads every user into memory. Could we paginate?"
6. Design and readabilityNaming, long functions, duplication, unclear responsibilities"Nit: d could be dueDate for clarity."

Writing good review comments

  • Be specific: point at the line and say exactly what could go wrong.
  • Ask questions instead of giving orders: "What happens if...?"
  • Explain why it matters and suggest a fix.
  • Label importance: "Blocking:", "Suggestion:", "Nit:" (a small, optional point).
  • Praise good parts: "Nice use of a discriminated union here."
  • Review the code, not the person.

Practise reviewing your own old code

A task from your study plan: take a file from one of your older projects and review it as if a stranger wrote it. Write the comments you would leave. You will almost certainly find real bugs, and you will have concrete stories for interviews.

How to say it in an interview

"I start from the entry point named in the report, skim names and types to build a map, then follow the data to where the value first goes wrong. I look for the root cause rather than patching the symptom, fix it, add a regression test, and check for the same pattern elsewhere. In code review I prioritise correctness, then security, data integrity, tests, performance and readability, and I write specific, kind comments that explain why and label how important they are."

Key words

WordMeaning
Root causeThe real underlying reason for a bug
SymptomThe visible effect of a bug
Regression testA test that stops a fixed bug from coming back
Code reviewReading a colleague's changes before they are merged
NitA minor, optional review comment

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.