Time: 0:00
Test a custom input to verify an edge case (e.g. empty lists, negative numbers, duplicates).
This bug belongs to the async and race conditions pattern
A missing await, unbuffered channel deadlocks, ConcurrentModificationException, and data races under load.
What went wrong: The WaitGroup.Add(1) call was inside the goroutine, which led to a race condition where the main goroutine might call Wait() before all goroutines had called Add(1).
The fix: Move the WaitGroup.Add(1) call before launching the goroutine to ensure it is called for each goroutine before they start.
Remember: Always increment the WaitGroup counter before launching a goroutine to avoid race conditions.
The pattern behind it: async and race conditions.
Paste this into any web page or blog post. Visitors fix the bug right there, in their browser.
<iframe src="https://trybughunt.com/embed/b0f003e0-63cb-4785-83bd-fa23f3827133" title="Debugging challenge: Goroutines finish before expected despite using sync.WaitGroup" 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/b0f003e0-63cb-4785-83bd-fa23f3827133">TryBugHunt</a></p>