Code Room
Code reviewHardcr-g220
Subject Lock misuseLevel Senior–Staff~40 minCommon in Concurrency interviewsIndustries Software development

Question

Review this Go cache with a read-then-upgrade pattern.

What's the concurrency bug?

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.

Talk through your review
Code to reviewgo
type Cache struct {    mu   sync.RWMutex    data map[string]int} func (c *Cache) GetOrCompute(k string) int {    c.mu.RLock()    if v, ok := c.data[k]; ok {        c.mu.RUnlock()        return v    }    // not found: upgrade to write lock while still holding the read lock    c.mu.Lock()                 // (1)    defer c.mu.Unlock()    v := compute(k)    c.data[k] = v    c.mu.RUnlock()    return v}
Run or narrate your approach, then ask the coach.