# Systematic Debugging Exercises

## Exercise 1: Write a Reproduction Recipe

**Task:** A user reports: "Sometimes the export fails." Write a reproduction recipe that would help you (or a teammate) reliably reproduce the issue. Include: steps, inputs, environment, and what "fails" means.

**Validation:**
- [ ] Steps are ordered and specific
- [ ] Inputs are concrete (e.g., "export 50+ items")
- [ ] Environment noted (browser, OS, data state)
- [ ] "Fails" is defined (error message, wrong output, hang)

**Hints:**
1. "Sometimes" suggests timing or data-dependent — capture those
2. "Export" — what format? What data?
3. Ask: when does it work vs fail?

---

## Exercise 2: Interpret a Stack Trace

**Task:** Given this stack trace:

```
RangeError: Invalid array length
    at Array.fill (native)
    at createGrid (grid.js:5:12)
    at initGame (game.js:22:8)
```

Identify: (a) where it failed, (b) the likely cause, (c) one line of code you'd inspect first.

**Validation:**
- [ ] Failure at grid.js:5, in createGrid
- [ ] Array.fill with invalid length — probably negative or NaN
- [ ] Would check the argument passed to createGrid (likely width/height)

**Hints:**
1. Array.fill(length) fails if length is negative or non-integer
2. createGrid probably receives dimensions
3. initGame calls createGrid — what does it pass?

---

## Exercise 3: Binary Search the Bug

**Task:** A function processItems(items) fails when items has 10,000 elements but works with 100. Describe how you'd binary search to find the threshold (e.g., does 5,000 work? 7,500?).

**Validation:**
- [ ] Describes halving: try 5000, then 2500 or 7500 based on result
- [ ] Continues until threshold is found
- [ ] Understands this finds "when" it breaks, not necessarily "why"

**Hints:**
1. Test 5000 — pass or fail?
2. If fail, try 2500; if pass, try 7500
3. Repeat until you narrow to the boundary

---

## Exercise 4: Hypothesize and Test

**Task:** Bug: "Form validation fails for emails with a '+' character." Write two hypotheses and a test for each.

**Validation:**
- [ ] Hypothesis 1 is specific (e.g., regex doesn't allow +)
- [ ] Hypothesis 2 is different (e.g., encoding strips +)
- [ ] Each test is concrete (e.g., input "a+b@c.com", expect pass)

**Hints:**
1. Validation could be regex, built-in validator, or backend
2. + is valid in email local part; some regexes exclude it
3. URL encoding could alter + if passed in query string

---

## Exercise 5: Cause vs Symptom

**Task:** Bug: "Dashboard shows 0 for today's revenue when there are sales." Proposed fix: "If revenue is 0, display '—' instead."

Explain why this might be a symptom fix. What would you investigate before or in addition to this change?

**Validation:**
- [ ] Identifies that 0 might be wrong (data/aggregation bug)
- [ ] Suggests investigating: API response, date range, aggregation logic
- [ ] Understands display fix can hide a data bug

**Hints:**
1. Is the data actually 0, or is it a bug in calculation?
2. Check API, database query, timezone
3. Display fix is OK for UX, but root cause may need fixing too
