TryBugHunt
goOtherHard

Every combo meal ends up with the last side dish

BugFind the bug and fix the code

What it should do

comboMeals(base, sides) builds one combo per side dish: the base items followed by that side. comboMeals(["burger", "drink"], ["fries", "salad"]) returns [["burger", "drink", "fries"], ["burger", "drink", "salad"]].

What happens instead

comboMeals(["burger", "drink"], ["fries", "salad"]) returns [["burger","drink","salad"],["burger","drink","salad"]].

package main

func comboMeals(base []string, sides []string) [][]string {
    meal := make([]string, 0, 10)
    meal = append(meal, base...)
    combos := [][]string{}
    for _, side := range sides {
        combo := append(meal, side)
        combos = append(combos, combo)
    }
    return combos
}

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)

meal has spare capacity, so append(meal, side) does not allocate: it writes side into the same backing array, just past the end of meal, and returns a slice that shares that array. Every combo is a view of the same memory, so each append overwrites the slot the previous combo was using, and they all show the last side. The bug depends on capacity, which is why it can appear or vanish when you change how a slice was built. To get an independent slice, copy first: append(append([]string{}, meal...), side).

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/8ff74223-00a2-4355-8281-522b10561852" title="Debugging challenge: Every combo meal ends up with the last side dish" 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/8ff74223-00a2-4355-8281-522b10561852">TryBugHunt</a></p>