TryBugHunt
javaScope errorHard

Similar titles check says every word is shared

BugFind the bug and fix the code

What it should do

sharedWords(a, b) counts the words in title a that also appear in title b, ignoring case, so sharedWords("red blue", "Blue green") returns 1.

What happens instead

sharedWords("red blue", "Blue green") returns 4, and sharedWords("cat", "dog") returns 2.

import java.util.*;

public class Main {
    static List<String> words = new ArrayList<>();

    static List<String> tokenize(String text) {
        for (String w : text.toLowerCase().split(" ")) {
            if (!w.isEmpty()) {
                words.add(w);
            }
        }
        return words;
    }

    static int sharedWords(String a, String b) {
        List<String> first = tokenize(a);
        List<String> second = tokenize(b);
        int count = 0;
        for (String w : first) {
            if (second.contains(w)) {
                count++;
            }
        }
        return count;
    }
}

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 scope and closure bugs pattern

A field shadowed by a local or parameter with the same name, so the field never changes.

What you'll practise

A hard Java debugging exercise about scope and closure bugs: a field shadowed by a local or parameter with the same name, so the field never changes.

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)

words is a static field, so there is only one list for the whole program. The first call to tokenize fills it with a's words, and the second call adds b's words to the same list and returns it again. first and second are therefore the same object, holding the words of both titles, so every word appears to be shared. A helper that builds and returns a result should create that result as a local variable, so each call gets its own. Static fields keep their state between calls, which is rarely what a pure helper wants. If two variables mysteriously always match, check whether they point to the same object.

The pattern behind it: scope and closure bugs.

More scope and closure bugs to practise

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/1895f2b6-ccb0-43d1-9a95-e9f91188475d" title="Debugging challenge: Similar titles check says every word is shared" 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/1895f2b6-ccb0-43d1-9a95-e9f91188475d">TryBugHunt</a></p>