TryBugHunt
goOtherHard

Adding a substitute to team A removes a player from team B

BugFind the bug and fix the code

What it should do

splitTeams(players, sub) puts the first half of the players in team A and the rest in team B, then adds the substitute to team A. splitTeams(["ann", "ben", "cat", "dan"], "sam") returns [["ann", "ben", "sam"], ["cat", "dan"]].

What happens instead

splitTeams(["ann", "ben", "cat", "dan"], "sam") returns [["ann","ben","sam"],["sam","dan"]]: cat has vanished from team B.

package main

func splitTeams(players []string, sub string) [][]string {
    half := len(players) / 2
    teamA := players[:half]
    teamB := players[half:]
    teamA = append(teamA, sub)
    return [][]string{teamA, teamB}
}

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

Slices that share one backing array, so changing a copy changes the original.

What you'll practise

A hard Go debugging exercise about mutation and copying bugs: slices that share one backing array, so changing a copy changes the original.

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)

teamA and teamB are two windows onto the same backing array, and teamA's capacity runs all the way to the end of players. Appending to teamA therefore does not allocate; it writes sam into the next slot, which is the first element of teamB. Nothing panics and both slices look valid, which makes this one of the hardest Go bugs to spot. Copy a sub-slice before appending to it, or limit its capacity with a full slice expression such as players[:half:half] so append is forced to allocate.

The pattern behind it: mutation and copying bugs.

More mutation and copying 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/4a30f4bf-4004-4251-bae3-5ba54d8c5012" title="Debugging challenge: Adding a substitute to team A removes a player from team B" 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/4a30f4bf-4004-4251-bae3-5ba54d8c5012">TryBugHunt</a></p>