Go Concurrency Patterns interview questions

25 questions from the Go Concurrency Patterns course, each with the correct answer and an explanation. Try to answer before you open the answer.

Concurrency Foundations in Go

1. What does GOMAXPROCS control?

  1. The size of each goroutine stack
  2. The maximum number of goroutines a program may create
  3. How many OS threads can execute Go code simultaneously (the number of Ps)
  4. How many channels can be open at once
Show answer

Answer: How many OS threads can execute Go code simultaneously (the number of Ps)

GOMAXPROCS limits parallel execution of Go code, not the number of goroutines. Threads blocked in system calls do not count against it.

Lesson: Concurrency, Parallelism and the Go Scheduler

2. What happens to running goroutines when main returns?

  1. The program exits and they are stopped without running further code
  2. main waits for them automatically
  3. They keep running in the background as a daemon
  4. The runtime panics with a deadlock error
Show answer

Answer: The program exits and they are stopped without running further code

Program exit does not wait for goroutines and does not run their deferred calls; you must wait for them explicitly.

Lesson: Goroutines and Their Lifecycle

3. When is a sync.Mutex usually the better choice than a channel?

  1. Streaming results between pipeline stages
  2. Handing work items from producers to consumers
  3. Signalling to many goroutines that they should stop
  4. Guarding a small piece of shared state, such as a counter or cache map
Show answer

Answer: Guarding a small piece of shared state, such as a counter or cache map

Channels shine for transferring ownership and signalling; mutexes are simpler for protecting state in place.

Lesson: Share Memory by Communicating

Channels in Depth

4. When does a send on a buffered channel with capacity 3 block?

  1. Always, until a receiver is ready
  2. When the buffer already holds 3 values
  3. Never, buffered sends are non-blocking
  4. Only after the channel is closed
Show answer

Answer: When the buffer already holds 3 values

A buffered send blocks only when the buffer is full.

Lesson: Unbuffered vs Buffered Channels

5. What does a receive from a closed, empty channel do?

  1. Blocks forever
  2. Returns immediately with the zero value and ok == false
  3. Panics
  4. Returns the last value that was sent
Show answer

Answer: Returns immediately with the zero value and ok == false

Only sending on or closing a closed channel panics; receiving is always safe.

Lesson: Closing Channels, Ranging and nil Channels

6. What happens if code calls close on a value of type <-chan int?

  1. It is silently ignored
  2. It panics at run time
  3. It closes the channel normally
  4. The program fails to compile
Show answer

Answer: The program fails to compile

Closing a receive-only channel is rejected by the compiler.

Lesson: Directional Channel Types in APIs

select, Timeouts and Timers

7. If two cases of a select are ready at the same time, which runs?

  1. One chosen pseudo-randomly
  2. Always the first one written
  3. Always the last one written
  4. Both, in order
Show answer

Answer: One chosen pseudo-randomly

Random choice among ready cases prevents systematic starvation; do not rely on case order for priority.

Lesson: select Basics and Non-blocking Operations

8. Why is time.After inside a busy for-select loop often a mistake for an overall timeout?

  1. time.After panics after the first use
  2. time.After cannot be used in select
  3. A new timer is created every iteration, so the timeout restarts whenever another case fires
  4. It makes the select choose cases in order
Show answer

Answer: A new timer is created every iteration, so the timeout restarts whenever another case fires

Create one timer before the loop, or use a context with a deadline.

Lesson: Timeouts, Timers and Tickers

9. Why set a closed input channel variable to nil inside a merge loop?

  1. It frees the channel memory immediately
  2. Setting it to nil reopens the channel
  3. A closed channel is always ready and returns zero values, while a nil channel case is never selected
  4. select panics on closed channels
Show answer

Answer: A closed channel is always ready and returns zero values, while a nil channel case is never selected

Without the nil assignment the loop would spin on zero values from the closed channel.

Lesson: The nil-Channel Trick

Synchronisation Primitives

10. Where should wg.Add(1) be called?

  1. In the parent goroutine, before the go statement
  2. Inside the new goroutine, as its first line
  3. After wg.Wait()
  4. Inside a deferred function
Show answer

Answer: In the parent goroutine, before the go statement

Adding inside the goroutine races with Wait, which may return before the counter is incremented.

Lesson: sync.WaitGroup

11. What happens if a goroutine calls Lock twice on the same sync.Mutex without unlocking?

  1. It panics with a reentrancy error
  2. The second Lock is a no-op
  3. The mutex counts the locks and needs two Unlocks
  4. It deadlocks, because Go mutexes are not reentrant
Show answer

Answer: It deadlocks, because Go mutexes are not reentrant

The second Lock waits for an Unlock that the same goroutine can never reach.

Lesson: Mutex and RWMutex

12. Which use case does the sync.Map documentation describe as a good fit?

  1. Any map shared between goroutines
  2. Keys that are written once and read many times
  3. Maps whose values must be updated together in transactions
  4. Small maps accessed by a single goroutine
Show answer

Answer: Keys that are written once and read many times

For general use a map plus a Mutex is usually simpler and type-safe.

Lesson: sync.Once, sync/atomic and sync.Map

Context and Cancellation

13. What does ctx.Err() return after a WithTimeout context expires?

  1. context.Canceled
  2. context.DeadlineExceeded
  3. nil
  4. An error created by time.After
Show answer

Answer: context.DeadlineExceeded

Explicit cancel() yields context.Canceled; an expired deadline yields context.DeadlineExceeded.

Lesson: context.Context Basics

14. Why must a producer goroutine also select on ctx.Done() when sending?

  1. Channels are closed automatically on cancellation
  2. Sending is not allowed without a context
  3. ctx.Done() makes the send faster
  4. If the consumer stops receiving, the send would block forever and leak the goroutine
Show answer

Answer: If the consumer stops receiving, the send would block forever and leak the goroutine

Cancellation is cooperative; every blocking point must be able to observe it.

Lesson: Propagating Cancellation

15. Which use of context.WithValue follows the documented guidance?

  1. Carrying a request ID through a call chain
  2. Passing the database connection pool
  3. Passing an optional retry count to one function
  4. Storing the context in a long-lived service struct
Show answer

Answer: Carrying a request ID through a call chain

Context values are for request-scoped data that crosses API boundaries.

Lesson: Context Values and the Rules

Core Concurrency Patterns

16. In a pipeline, which stage should close a given channel?

  1. The main function, always
  2. The stage that receives from it
  3. The stage that sends on it (its owner)
  4. No one; channels must never be closed
Show answer

Answer: The stage that sends on it (its owner)

Owners close their outputs so downstream range loops terminate.

Lesson: Pipelines

17. What is lost when a stage is fanned out to several workers?

  1. Channels can no longer be closed
  2. Values may be delivered to two workers at once
  3. The original ordering of the results
  4. Context cancellation stops working
Show answer

Answer: The original ordering of the results

Each value goes to exactly one receiver, but completion order varies.

Lesson: Fan-out and Fan-in

18. How does a buffered channel act as a semaphore?

  1. Its capacity limits how many tokens can be held, so sends block once N workers are active
  2. Buffered channels automatically limit the number of goroutines in the program
  3. Closing the channel releases all workers at once
  4. The runtime schedules only cap(ch) goroutines
Show answer

Answer: Its capacity limits how many tokens can be held, so sends block once N workers are active

Send to acquire, receive to release; capacity is the limit.

Lesson: Worker Pools and Bounded Concurrency

Errors, Rate Limits and Shutdown

19. What does g.Wait() return when two tasks in an errgroup fail?

  1. nil, because errors are only logged
  2. Both errors joined together
  3. The last error returned
  4. The first non-nil error returned by any task
Show answer

Answer: The first non-nil error returned by any task

errgroup keeps the first error; use errors.Join yourself if you need all of them.

Lesson: errgroup for Concurrent Tasks with Errors

20. In rate.NewLimiter(r, b), what does b control?

  1. The number of goroutines that may call Wait
  2. The bucket size, i.e. the maximum burst of events allowed at once
  3. The timeout for Wait
  4. The number of seconds between tokens
Show answer

Answer: The bucket size, i.e. the maximum burst of events allowed at once

r is the refill rate; b is the burst size.

Lesson: Rate Limiting

21. What does http.Server.ListenAndServe return after Shutdown is called?

  1. nil
  2. http.ErrServerClosed
  3. context.Canceled
  4. It never returns
Show answer

Answer: http.ErrServerClosed

Treat ErrServerClosed as a normal exit, not a failure.

Lesson: Graceful Shutdown

Correctness in Production

22. Which statement about the Go race detector is accurate?

  1. It detects deadlocks rather than races
  2. It proves a program is race-free
  3. It runs at no extra cost, so it should always be on in production
  4. It reports only races that actually happen during the instrumented run
Show answer

Answer: It reports only races that actually happen during the instrumented run

No false positives, but no guarantee either: coverage matters.

Lesson: Data Races and the Race Detector

23. When does the Go runtime itself report "all goroutines are asleep - deadlock!"?

  1. Only when every goroutine in the program is blocked
  2. Whenever any two goroutines wait on each other
  3. Whenever a goroutine blocks for more than a second
  4. Whenever a channel is left open at exit
Show answer

Answer: Only when every goroutine in the program is blocked

Partial deadlocks and leaks go unreported, which is why profiles and leak tests matter.

Lesson: Goroutine Leaks and Deadlocks

24. Inside a testing/synctest bubble, when does the fake clock advance?

  1. Only when the test calls time.Sleep in the main goroutine
  2. Every real millisecond, like the normal clock
  3. When every goroutine in the bubble is durably blocked
  4. Never; timers inside a bubble are disabled
Show answer

Answer: When every goroutine in the bubble is durably blocked

Time jumps forward to the next timer once nothing in the bubble can make progress, making timeouts deterministic.

Lesson: Testing Concurrent Code

25. Which item belongs on a Go concurrency review checklist?

  1. Store the request context in the service struct
  2. Use large channel buffers to avoid deadlocks
  3. Every goroutine has a defined way to stop and someone waiting for it
  4. Use time.Sleep in tests to let goroutines finish
Show answer

Answer: Every goroutine has a defined way to stop and someone waiting for it

Clear goroutine lifetimes prevent leaks, races at exit and lost work.

Lesson: A Go Concurrency Checklist

← Back to the course