Language: Python
python
def get_average(scores):
total = 0
for score in scores:
total += score
return total / len(scores)
print(get_average([]))
What breaks here? Try to spot it before reading on π
.
.
.
The bug:
ZeroDivisionError when scores is empty. It's an easy edge case to forget under interview pressure, but interviewers plant empty-input tests specifically to see if you check for them.Fixed version:
python
def get_average(scores):
if not scores:
return 0 # or raise a meaningful exception, depending on requirements
return sum(scores) / len(scores)
π‘ Takeaway: Before you write a single line of code in an interview, say out loud: "What happens with an empty input? A single element? Negative numbers?" It shows structured thinking, and it catches bugs like this before they exist.