Challenge - 5 Problems
Infinite Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Detecting infinite loop with a missing update
What will be the output of this code snippet?
Python
count = 0 while count < 3: print(count) # Missing count update here
Attempts:
2 left
💡 Hint
Check if the loop variable changes inside the loop.
✗ Incorrect
The variable 'count' is never updated inside the loop, so the condition 'count < 3' is always true, causing an infinite loop printing 0 repeatedly.
❓ Predict Output
intermediate2:00remaining
Loop with break to prevent infinite iteration
What is the output of this code?
Python
i = 0 while True: print(i) i += 1 if i == 3: break
Attempts:
2 left
💡 Hint
Look for the break condition inside the loop.
✗ Incorrect
The loop prints 0, 1, 2 and then breaks when i reaches 3, so it stops before printing 3.
❓ Predict Output
advanced2:00remaining
Infinite loop caused by wrong loop condition update
What will be the output of this code?
Python
n = 5 while n > 0: print(n) n += 1
Attempts:
2 left
💡 Hint
Check if the loop variable moves towards the exit condition.
✗ Incorrect
The variable 'n' increases each time, so 'n > 0' is always true, causing an infinite loop printing increasing numbers starting from 5.
❓ Predict Output
advanced2:00remaining
Infinite loop due to mutable default argument in recursion
What will happen when this function is called as 'func()'?
Python
def func(lst=None): if lst is None: lst = [] lst.append(1) print(lst) if len(lst) < 3: func(lst) func()
Attempts:
2 left
💡 Hint
Look at how the list grows and when recursion stops.
✗ Incorrect
The function appends 1 to the list and calls itself until the list length reaches 3, then stops, printing the list at each step.
🧠 Conceptual
expert2:00remaining
Best practice to prevent infinite loops in complex code
Which option is the best practice to prevent infinite loops in complex programs?
Attempts:
2 left
💡 Hint
Think about how to stop loops that might run too long.
✗ Incorrect
Including a maximum iteration count or timeout ensures loops stop even if the main exit condition fails, preventing infinite loops in complex or unexpected cases.