Code RoomWorker reads wrong shard index
MediumPrep Room Coding #4949

Worker reads wrong shard index

Code reviewConcurrencyMid–Senior~25 min

Review this Go code (built with Go 1.21) that launches per-shard workers.

There are two distinct concurrency bugs. Identify both.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 25 min
Mark a line and say what kind of problem it is.0 findings
1func fanOut(shards []*Shard, results map[int]int) {
2 var wg sync.WaitGroup
3 for i := 0; i < len(shards); i++ {
4 wg.Add(1)
5 go func() {
6 defer wg.Done()
7 n := shards[i].Count() // (1)
8 results[i] = n // (2)
9 }()
10 }
11 wg.Wait()
12}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.