
Understand it
Do not edit randomly during an interview. Restate the symptom, identify the first impossible state, propose one hypothesis, and choose the smallest observation that can disprove it.
Start by restating the symptom in observable terms: which input, expected result, actual result, and whether the failure is repeatable. Separate a compile failure, runtime panic, wrong output, and performance regression because each suggests a different search path. Form one hypothesis that would explain the evidence. Then choose the smallest observation that could disprove it. Reading a diagnostic span or printing one value at a boundary is more useful than making three speculative edits at once.
Suppose a function returns an empty list only on the second call. Ask whether state is reused between calls, whether the input differs, and whether a cache has an invalid key. Instrument before and after the suspected state change. If the values refute the hypothesis, update the model rather than defending the first guess. Reduce the failing case until one invariant becomes obvious. In Rust, ownership often prevents a class of memory errors, but application logic and concurrency ordering can still fail.
During an interview, narrate the experiment and its result. A good answer says 'if X is true, this observation should be Y; otherwise I will inspect Z.' Once the cause is found, propose a minimal fix and a regression test for the exact boundary. Also consider how the issue escaped earlier checks. The interviewer is evaluating disciplined reasoning under uncertainty, not whether you can guess the bug in one sentence.
Interview follow-up: Return Option<u32> with a safely widened accumulator, then test empty input, maxima and truncation of a fractional mean.
Read the code
fn average(values: &[u32]) -> u32 {
let total: u32 = values.iter().sum();
total / values.len() as u32
}An empty slice makes the denominator zero, so this division panics. The sum can also overflow.
Practice starter
fn average(values: &[u32]) -> u32 {
let total: u32 = values.iter().sum();
total / values.len() as u32
}
fn main() { println!("{}", average(&[])); }Try it
Reproduce the panic from average(&[]), state one hypothesis, and name the smallest observation that could disprove it.
What success looks like: You name the zero denominator from evidence, then propose a fix and a regression test for empty input.
Check your understanding
What failure does average(&[]) reveal in this implementation?
Reveal the answer
- The denominator is zero, so integer division panics.
- Borrowing an empty slice is itself undefined behaviour.
- The compiler rejects every call passing an empty slice to this function.
Define empty-input behaviour before patching. Also investigate sum overflow, narrowing len and the intended rounding policy.
Go deeper
Common misconception: Borrowing an empty slice is itself undefined behaviour.
&[] is a valid empty slice, and borrowing it is fine. The panic comes later, from dividing by values.len() as u32, which is zero.
- Identify the contract: Form a hypothesis and request one discriminating fact.
- Trace the concrete example: An empty slice makes the denominator zero, so this division panics. The sum can also overflow.
- Test the changed constraint: Return Option<u32> with a safely widened accumulator, then test empty input, maxima and truncation of a fractional mean.
Transfer challenge: Return Option<u32> with a safely widened accumulator, then test empty input, maxima and truncation of a fractional mean.
Connections
- Design a Parser: Separate tokenization, grammar, and diagnostics
- Communicate a Tradeoff: Tie a choice to constraints and a failure mode
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Debugging Out Loud (courseId "interview", lessonId "q-debugging-session") Link: https://crabrix.com/learn/interview/q-debugging-session/ Goal: Do not edit randomly during an interview. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-debugging-session) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.