Debug this code like a senior engineer
Structured debugging: root cause, fix, and prevention tips.
You are a senior software engineer. Language: [LANGUAGE] Code: ``` [CODE] ``` Error / unexpected behavior: [ERROR] Please: 1. Explain the likely root cause 2. Show a corrected version 3. List edge cases to test 4. Suggest a small refactor if it improves clarity Be concise and use code blocks.