Level: AdvancedInterview

Go Goroutines Interview Questions: Scheduler and Leaks (Part 14)

Updated 9 min read
Go Goroutines Interview Questions featured image: go keyword, scheduler and WaitGroup cards

These Go goroutines interview questions cover the go keyword, the scheduler, GOMAXPROCS, WaitGroup, goroutine leaks, data races and the memory model. We gathered them from public interview lists, coding rounds and community threads. Then we rewrote them in plain words and grouped them by idea.

This post is questions only, so answer each one in your own words first. When you need help, read Go goroutines for beginners, Go WaitGroup and Mutex and Go race detector. Also, finish Part 13 on testing first, because the race detector runs through go test.

How to Use These Go Goroutines Interview Questions

Answer each of these Go goroutines interview questions out loud, then run a small program with the -race flag. Concurrency bugs often hide on your laptop and appear only under real load. Finally, try the scenario questions, since they come from real production incidents.

The table below lists each section and its number of questions.

SectionWhat it testsQuestions
Goroutine basicsThe go keyword and goroutine life15
The schedulerG, M, P and preemption15
Waiting for workWaitGroup, results and closures15
Leaks and lifecycleStopping, cancelling and counting goroutines15
Races and memoryThe race detector and happens before14
Goroutine coding tasksClassic interview programs14
Go Goroutines Interview Questions diagram showing six question areas from the go keyword to coding tasks
The six areas covered in this question list, from the go keyword to coding tasks.

Go Goroutines Interview Questions on the Basics

Before you answer, study Go goroutines for beginners. These Go goroutines interview questions check the rules every Go developer must know before writing concurrent code.

  1. What is a goroutine, and how do you start one?
  2. How is a goroutine different from an OS thread?
  3. Why are goroutines cheap, for example in stack size and creation time?
  4. What happens to running goroutines when main returns?
  5. Why is time.Sleep a bad way to wait for a goroutine?
  6. Can a goroutine return a value, and how do you get its result back?
  7. What happens when a goroutine panics and nobody recovers?
  8. Does Go give goroutines an ID, and why did the designers hide it?
  9. What is the difference between concurrency and parallelism?
  10. How many goroutines can a program run, and what limits that number?
  11. What does runtime.Gosched do, and why do you rarely need it?
  12. How does a goroutine stack grow when it needs more space?
  13. When does starting a goroutine make a program slower instead of faster?
  14. Scenario: when a program prints nothing from its goroutines, what is the most likely cause?
  15. Scenario: when a service starts one goroutine per request and memory keeps rising, what would you check?

Go Goroutines Interview Questions on the Scheduler

Before you answer, study Go profiling with pprof, because traces and profiles show the scheduler at work. These Go goroutines interview questions are common for senior backend roles.

  1. What do G, M and P mean in the Go scheduler?
  2. Where does a new goroutine wait to run, in a local run queue or the global one?
  3. What is work stealing, and why does it help?
  4. What does GOMAXPROCS control, and what is its default value?
  5. How did Go 1.25 change GOMAXPROCS for containers with CPU limits?
  6. What happens to a P when a goroutine makes a blocking system call?
  7. How does the network poller keep goroutines cheap during socket waits?
  8. What is preemption, and why did Go add asynchronous preemption in Go 1.14?
  9. Why could a tight loop block other goroutines in old Go versions?
  10. How does runtime.LockOSThread change scheduling, and when do you need it?
  11. How does cgo affect the number of OS threads?
  12. What does the execution tracer show that a CPU profile does not?
  13. Why does a high number of runnable goroutines hint at CPU starvation?
  14. Scenario: when a Go service in Kubernetes uses far more threads than expected, what would you check about GOMAXPROCS?
  15. Scenario: when latency spikes appear while CPU is low, how would you use a trace to find blocked goroutines?

Go Goroutines Interview Questions on Waiting

Before you answer, study Go WaitGroup and Mutex and Go worker pool pattern. Interviewers love these Go goroutines interview questions, because many real bugs start with a missing wait.

  1. How does sync.WaitGroup work, and what do Add, Done and Wait do?
  2. Why must you call Add before you start the goroutine?
  3. What happens when the WaitGroup counter goes below zero?
  4. Why should you pass a WaitGroup by pointer?
  5. What does WaitGroup.Go add in Go 1.25?
  6. How do you collect results from many goroutines into one slice safely?
  7. When is writing to different indexes of one slice from many goroutines safe?
  8. Which package helps you collect the first error from a group of goroutines, and how?
  9. How does errgroup.SetLimit cap the number of running goroutines?
  10. How did the loop variable bug affect goroutines started in a loop before Go 1.22?
  11. Why should you pass values into a goroutine as arguments in older code?
  12. How do you wait for goroutines with a timeout?
  13. What is the difference between waiting with a channel and waiting with a WaitGroup?
  14. Scenario: when a batch job sometimes returns before all workers finish, what would you look for?
  15. Scenario: when one of a hundred goroutines fails, how would you stop the rest quickly?

Go Goroutines Interview Questions on Leaks

Before you answer, study Go graceful shutdown and Go context for beginners. Leaks are silent, so these Go goroutines interview questions check how you stop work you started.

  1. What is a goroutine leak, and why does it waste memory?
  2. Which patterns cause leaks, for example a send on a channel that nobody reads?
  3. Can runtime.NumGoroutine detect a leak in a test, and how?
  4. How do tools such as goleak help in unit tests?
  5. How does a goroutine profile in pprof show leaked goroutines?
  6. Why should every goroutine have a clear way to stop?
  7. How does a done channel tell a goroutine to stop?
  8. How does context cancellation stop a tree of goroutines?
  9. Why can you not kill a goroutine from the outside?
  10. What happens to goroutines blocked on a closed network connection?
  11. How do you shut down background workers when a server stops?
  12. Why can a buffered channel of size one fix some leaks?
  13. How do you avoid leaks with time.After inside a loop in older Go versions?
  14. Scenario: when a goroutine profile shows ten thousand goroutines stuck in chan send, what would you do?
  15. Scenario: when a test passes but goleak reports a leftover goroutine, how would you find its owner?

Data Races and the Memory Model

Before you answer, study Go race detector. These Go goroutines interview questions check whether you know when two goroutines can safely touch the same memory.

  1. What is a data race, and how is it different from a race condition?
  2. Why is a data race undefined behaviour in Go programs?
  3. How does the -race flag find races, and what is its cost?
  4. What does happens before mean in the Go memory model?
  5. Which operations create a happens before edge, for example a channel send?
  6. Why is reading a bool flag from another goroutine without sync unsafe?
  7. Can concurrent map reads and writes crash a program, and what message appears?
  8. Is it safe for many goroutines to read the same map with no writers?
  9. When would you use sync/atomic instead of a mutex?
  10. What does the atomic.Int64 type add over the old atomic functions?
  11. Why is a counter with ++ from many goroutines wrong?
  12. How do you share data by communicating instead of communicating by sharing data?
  13. Scenario: when the race detector reports a race in a logging helper, how would you fix it?
  14. Scenario: when a service panics with concurrent map writes in production, what would you change?

Go Goroutines Interview Questions for Coding Rounds

Before you answer, write each program by hand and run it with -race. Interviewers watch how you stop and wait for goroutines, not only whether the code runs.

  1. Start ten goroutines that each print their number, then wait for all of them.
  2. Sum a large slice in four parts with four goroutines.
  3. Build a worker pool with three workers and ten jobs.
  4. Fetch five URLs at the same time and print each status code.
  5. Print odd and even numbers in order with two goroutines.
  6. Write a function that runs a task with a timeout.
  7. Limit the number of running goroutines with a semaphore channel.
  8. Count words in many files at the same time and merge the results.
  9. Fix a program with a data race on a shared counter.
  10. Write a test that fails when a function leaks a goroutine.
  11. Run three tasks and return the first error with errgroup.
  12. Build a simple rate limited loop that starts at most five jobs per second.
  13. Scenario: when an interviewer asks you to process a million records with limited memory, how would you size the worker pool?
  14. Scenario: when two goroutines must take turns printing, how would you coordinate them without sleep?

Code Reading Questions

Read each program carefully and predict the output before you run it. Then explain the result in your own words.

package main

import (
    "fmt"
    "sync"
    "sync/atomic"
)

func main() {
    var wg sync.WaitGroup
    results := make([]int, 5)
    for i := range 5 {
        wg.Add(1)
        go func() {
            defer wg.Done()
            results[i] = i * i
        }()
    }
    wg.Wait()
    fmt.Println(results)

    var total atomic.Int64
    for _, v := range results {
        wg.Add(1)
        go func(n int) {
            defer wg.Done()
            total.Add(int64(n))
        }(v)
    }
    wg.Wait()
    fmt.Println("total:", total.Load())
}
  1. What do the two print lines show, and why is writing to results[i] safe here?
  2. What would change if total were a plain int with total += n, and how would -race report it?

The second program shows a leak and an early exit from main.

package main

import (
    "fmt"
    "runtime"
    "time"
)

func leaky() {
    ch := make(chan int)
    go func() {
        ch <- 42
    }()
}

func main() {
    before := runtime.NumGoroutine()
    for range 3 {
        leaky()
    }
    time.Sleep(50 * time.Millisecond)
    fmt.Println(before, runtime.NumGoroutine())

    done := make(chan struct{})
    go func() {
        fmt.Println("worker finished")
        close(done)
    }()
    <-done
    go fmt.Println("this line may never print")
}
  1. What numbers does the first line print, and why do three goroutines stay alive forever?
  2. Why may the last line never print, and how would you fix leaky with one small change?

Quick Revision Table for Goroutines

Use the table below as a final check before your interview. It lists the tools that keep goroutines safe and under control.

ToolWhat it doesUse when
go keywordStarts a goroutineWork can run at the same time
sync.WaitGroupWaits for a group to finishYou start a known set of workers
errgroup.GroupWaits and returns the first errorWorkers can fail
context.ContextCancels a tree of workThe caller gives up or times out
sync/atomicUpdates one number safelyMany goroutines change a counter
go test -raceFinds data racesEvery CI run

Also, many interviewers ask you to compare goroutines and threads directly. The diagram below shows the difference side by side.

Go goroutines diagram comparing a goroutine with an OS thread
Goroutines are small and scheduled by the Go runtime, while OS threads are larger and scheduled by the kernel.

Official Resources

These official pages explain the rules behind these questions.

Quick Recap

  • These Go goroutines interview questions cover basics, the scheduler, waiting, leaks, races and coding tasks.
  • A goroutine is a small function run by the Go runtime, and main exit stops them all.
  • The scheduler runs goroutines on threads with G, M and P and steals work between Ps.
  • Wait with WaitGroup or errgroup, never with time.Sleep.
  • Every goroutine needs a way to stop, so use a done channel or a context.
  • Run tests with -race, because data races are bugs even when output looks right.

What’s Next?

By now, you have Go goroutines interview questions for every part of goroutines and the scheduler. Study the answers in your own notes, then check them against the Go interview topics study guide.

Next up: Go channels interview questions (Part 15)