BugHunt

Infinite loops: why your program hangs and never finishes

The tab freezes, nothing prints, and the loop condition never becomes false.

12 free challenges6 Python6 JavaScript

What is infinite loop?

An infinite loop is a loop whose exit condition never becomes true. The program does not crash and does not print an error — it simply stops making progress, which makes it one of the more disorienting bugs to hit for the first time.

Why it happens

Every loop needs its body to move something toward the exit condition. The failure is almost always that the variable in the condition is not the variable the body changes: the increment sits inside an if, a continue jumps over it, a string method's return value is discarded so the text never actually changes, or a condition is tested against a value nothing updates.

How to recognise it

  • It hangs on some inputs and finishes on others — the working ones avoid the branch that fails to make progress.
  • A while loop whose body contains continue.
  • A condition on one variable while the body updates a different one.
  • In Python, calling text.replace(...) or list.sort() without assigning the result.

Errors and symptoms this causes

  • while loop never ends
  • page freezes / tab not responding
  • program hangs with no output
  • RecursionError: maximum recursion depth exceeded

How to fix it

Print the condition variable at the top of every iteration. If it does not change, you have found it. Ask what happens when the branch is false: if the answer is 'nothing', the loop can stall. Put the increment where it always runs — a for loop's header is safer than a while loop's body.

Practise infinite loops

Working code with one bug in it. Find it, fix it in the browser, and see the explanation. No account needed.

javascriptEasy
Doubling loop that never doubles

stepsToExceed(limit) counts how many times you must double 1 before passing limit, so stepsToExceed(8) returns 4.

Infinite loop

pythonEasy
Retry flag compared instead of assigned

attempts_until_success(n) retries until the nth attempt succeeds and returns the number of tries, so attempts_until_success(3) returns 3.

Infinite loop

javascriptHard
Duplicate removal spins forever

dedupe(arr) should return the array with duplicates removed, keeping the first occurrence of each value.

Infinite loop

pythonHard
Outer index advances only when a pair is found

count_pairs(nums, target) should count the pairs that add up to target, so count_pairs([1, 2, 3], 4) returns 1.

Infinite loop

javascriptHard
Binary search that stops narrowing

indexOfValue(sorted, target) should return the index of target in a sorted array, or -1 if it is absent.

Infinite loop

javascriptMedium
Reversing a string hangs the tab

reverseString(text) should return the string backwards, so "abc" becomes "cba".

Infinite loop

javascriptMedium
continue skips past the increment

countOdds(nums) should count the odd numbers, so countOdds([1, 2, 3]) returns 2.

Infinite loop

pythonMedium
Digit sum never finishes

sum_digits(n) should add up the digits of a number, so sum_digits(123) gives 6.

Infinite loop

pythonMedium
Halving loop with no plan for odd numbers

shrink_to_one(n) applies the Collatz rule — halve even numbers, and turn odd n into 3n + 1 — counting steps until n reaches 1. shrink_to_one(6) returns 8.

Infinite loop

javascriptMedium
Index only advances on a match

countUppercase(text) should count the uppercase letters, so countUppercase("aBc") returns 1.

Infinite loop

pythonMedium
Cleaning double spaces never terminates

squash_spaces(text) should collapse runs of spaces into one, so squash_spaces("a b") returns "a b".

Infinite loop

pythonMedium
Countdown that never ends

countdown(n) should return a list counting down from n to 1, e.g. countdown(3) -> [3, 2, 1].

Infinite loop

Other bug patterns