TryBugHunt
javaOtherMedium

Palindrome check rejects the word level

BugFind the bug and fix the code

What it should do

isPalindrome(word) returns true when the word reads the same backwards, ignoring case, so isPalindrome("Level") returns true and isPalindrome("hello") returns false.

What happens instead

isPalindrome("Level") returns false, and so does isPalindrome("a").

public class Main {
    static boolean isPalindrome(String word) {
        String clean = word.toLowerCase();
        return new StringBuilder(clean).reverse().equals(clean);
    }
}

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 mutation and copying bugs pattern

Two references to one object, so changing one changes both; shallow copies of lists.

What you'll practise

A medium Java debugging exercise about mutation and copying bugs: two references to one object, so changing one changes both; shallow copies of lists.

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)

StringBuilder.reverse() returns the StringBuilder itself, not a String. Comparing a StringBuilder with a String using equals is always false, because StringBuilder does not override equals to compare text, and a String is never equal to an object of another class anyway. The compiler accepts it because equals takes any Object. Call toString() first so you compare two Strings, or use clean.contentEquals(builder), which compares the characters of any CharSequence. When equals is false for values that look identical when printed, check that both sides really are the same type.

The pattern behind it: mutation and copying bugs.

More mutation and copying bugs to practise

Related errors

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/d39c8967-41dd-4395-b322-f0dd0b460f39" title="Debugging challenge: Palindrome check rejects the word level" 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/d39c8967-41dd-4395-b322-f0dd0b460f39">TryBugHunt</a></p>