TryBugHunt
goOff-by-oneEasy

Palindrome check crashes on every word longer than one letter

BugFind the bug and fix the code

What it should do

isPalindrome(word) reports whether a lowercase word reads the same forwards and backwards. isPalindrome("level") returns true and isPalindrome("hello") returns false.

What happens instead

isPalindrome("level") crashes with panic: runtime error: index out of range [5] with length 5.

package main

func isPalindrome(word string) bool {
    for i := 0; i < len(word)/2; i++ {
        if word[i] != word[len(word)-i] {
            return false
        }
    }
    return true
}

Test a custom input to verify an edge case (e.g. empty lists, negative numbers, duplicates).

⏱️0:00
Hints
🏆 0 hints = Flawless Solve
Think & Test Cooldown
2:00 remaining

Take a moment to read the bug symptom and trace the code with Run & Check before unlocking Hint 1.

Hint 2
Unlocks after Hint 1
Hint 3
Unlocks after Hint 2

This bug belongs to the off-by-one errors pattern

The loop that runs one time too many, the slice that drops the last item, the index that starts in the wrong place.

What you'll practise

An easy Go debugging exercise about off-by-one errors: the loop that runs one time too many, the slice that drops the last item, the index that starts in the wrong place.

The checks your fix must pass

Signs of this kind of bug

Spoiler: why this bug happens(try it first; the hints get warmer one at a time)

The character that mirrors index i is at len - 1 - i, because the last valid index of a 5-byte string is 4, not 5. On the first pass i is 0 and the code reads word[5], which does not exist, so Go panics before comparing anything. Single-letter words pass only because the loop never runs. Whenever you index from the end, the last element is len - 1; writing len - i is the same mistake as using len itself as an index.

The pattern behind it: off-by-one errors.

More off-by-one errors to practise

Seen this error in your own code?

Embed this challenge on your site

Paste this into any web page or blog post. Visitors fix the bug right there, in their browser.

<iframe src="https://trybughunt.com/embed/ced2266d-602b-4c5c-9f9c-3db8a48d4817" title="Debugging challenge: Palindrome check crashes on every word longer than one letter" width="100%" height="580" loading="lazy" style="border:1px solid #d4d4d8;border-radius:12px;max-width:760px"></iframe>
<p style="font-size:14px">Debugging challenge from <a href="https://trybughunt.com/challenges/ced2266d-602b-4c5c-9f9c-3db8a48d4817">TryBugHunt</a></p>