Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

Go Module 15

Concurrency patterns in Go interview questions

Interview questions on concurrency patterns in Go: short warm-ups, then what each harder question tests, the first step of a good answer and its follow-ups.

14 questions: 3 warm-ups and 11 with model answers · Levels: Fresher to staff

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher

    In a pipeline, which goroutine closes a channel, and why that one?

    Answered in the lesson Pipelines, fan-out and fan-in

  2. Fresher to mid

    How does a buffered channel of struct{} work as a semaphore, and where do you take the slot?

    Answered in the lesson Worker pools, semaphores and bounded parallelism

  3. Fresher to mid

    What is a goroutine leak, and why does the Go runtime not report it as a deadlock?

    Answered in the lesson Goroutine leaks and the goroutineleak profile

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Mid to senior

    A handler streams records through a three-stage pipeline and returns at the first invalid record. Since that change shipped, the service's goroutine count climbs by about three for every failed request. Explain why, and change the pipeline so that it cannot happen.

    What it tests
    Recognising senders left blocked after an early return, and stopping every stage through a context instead of relying on buffers.
    First step
    Count the stages: when the handler stops reading, each stage that holds a value waits in its send for ever, one goroutine per stage, which matches three per failed request. Then decide how every stage can learn that nobody reads any more.
    Follow-ups

    From the lesson Pipelines, fan-out and fan-in

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.

    Opening the model answers with your pass…

    Model answers open with any MySmartCoPilot pass.

    • Pro pass · 1 week₹79$2.99
    • 30 days₹179$5.99
    • 1 year₹1,699$45

    One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

    It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  2. Mid to staff

    An enrichment stage was fanned out to 16 workers to cut latency, and now the downstream report comes out in a different order on every run. Restore the input order without giving up the speed-up. How much memory can your fix hold at worst?

    What it tests
    Carrying an index through a fan-out, reordering with a buffer that is bounded, and reasoning about a slow item that holds back everything after it.
    First step
    Tag each item with its position before the fan-out, and let a reorder stage send item n only after items 0 to n-1. Then ask what limits the number of items it holds while one slow item is still being worked on.
    Follow-ups

    From the lesson Pipelines, fan-out and fan-in

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  3. Mid to senior

    An upload service starts one goroutine per uploaded photo, and each goroutine calls an image API that accepts at most 20 requests at once. A marketing email brings 5,000 uploads in a minute, and most of them fail. Explain why, and redesign the service.

    What it tests
    Seeing that unbounded goroutines move the limit downstream, and choosing a bound, a queue and an answer for the work that cannot be queued.
    First step
    Compare the number of calls the burst puts in flight with the 20 the API accepts: thousands of goroutines call at once, so the API refuses nearly all of them. Then decide where the waiting should happen instead, and what a user hears when there is no room to wait.
    Follow-ups

    From the lesson Worker pools, semaphores and bounded parallelism

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  4. Senior to staff

    A Go service must make 1,500 calls a second to a payments API whose median latency is 80 ms and whose p99 is 400 ms. How many workers do you give the pool, and what happens to that choice when the API's latency doubles during an incident?

    What it tests
    Turning a rate and a latency into the number of calls in flight, choosing which latency to size for, and knowing what a fixed pool does to throughput when latency rises.
    First step
    Start from the rate and from how long each call keeps a worker busy, and work out how many calls are in flight at once on average. Then ask which latency that average needs, the median or something higher, and whether the payments API itself accepts that many calls at once.
    Follow-ups

    From the lesson Worker pools, semaphores and bounded parallelism

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  5. Mid to senior

    On every deploy, a service with an HTTP server and a worker pool loses some queued jobs, and once in a while it panics with "send on closed channel". Walk through a shutdown that loses nothing, and say what you do when the deadline is too short to drain the queue.

    What it tests
    Finding why a send on a closed channel happens at shutdown, stopping the parts of a service in a safe order, and accounting for the jobs that cannot run before the deadline.
    First step
    Find who closes the jobs channel and when. The panic means a handler sent on the queue after it was closed, so the queue was closed while requests were still being served. Then trace what happens to the jobs still queued when the process exits.
    Follow-ups

    From the lesson Worker pools, semaphores and bounded parallelism

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  6. Mid to senior

    A Go service's memory climbs steadily between deploys and drops after each restart, and runtime.NumGoroutine climbs the same way. How do you find the leak, and what do you look for in the code once you know where the goroutines wait?

    What it tests
    Telling a goroutine leak from other memory growth, using the goroutineleak and goroutine profiles to find where goroutines wait, and recognising the code shapes that cause it.
    First step
    Confirm that the goroutine count, not only memory, grows with time or with failed requests. Then take the goroutineleak profile from the service's internal pprof endpoint and read where the leaked goroutines wait: the function and line point at the channel whose other side is gone.
    Follow-ups

    From the lesson Goroutine leaks and the goroutineleak profile

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  7. Senior to staff

    runtime.NumGoroutine rises by thousands a day, but the goroutineleak profile reports total 0. Which kinds of leak can hide from the profile, and how do you find this one?

    What it tests
    Knowing how the profile decides that a goroutine has leaked, which leaks that rule cannot see, and how to find them with other tools.
    First step
    Recall what the profile can know: whether anything that can still run could wake a blocked goroutine. Then ask what kinds of goroutine never return without being blocked on something unreachable, and which other profile shows every goroutine with its stack.
    Follow-ups

    From the lesson Goroutine leaks and the goroutineleak profile

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  8. Mid to senior

    After goleak.VerifyNone was added to every test of a package, its tests fail in different places depending on which tests run, and some tests of correct code fail. Explain what happens, and set up leak checks whose failures point at the test that leaked.

    What it tests
    Understanding what goleak checks and when, and arranging leak checks so that each failure names the test that caused it.
    First step
    Ask how long a leaked goroutine lives and what goleak can know about where a goroutine came from. Then look at which tests fail first when the package's tests run in a different order.
    Follow-ups

    From the lesson Goroutine leaks and the goroutineleak profile

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 2 minutes
  9. Mid to senior

    A deploy empties the cache, then 5,000 requests a second hit product pages whose entries are all cold, and the database falls over. Explain the stampede, fix it in Go, and say what the waiting requests get when a shared load fails.

    What it tests
    Recognising duplicate loads of one key, collapsing them per key, handling failed and slow shared loads, and keeping the cache and the collapsing apart.
    First step
    Count how many requests for each hot key miss the cache while its first load is still running: each one sends its own query. Then decide how the callers of one key can share one load, and what a shared failure means for them.
    Follow-ups

    From the lesson Concurrent caches, singleflight and pub/sub

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  10. Senior to staff

    A Go broker sends price updates to 2,000 WebSocket subscribers, one channel send each, and a slow mobile client makes every update late for everyone. Explain why, and redesign delivery so that a slow client hurts only itself.

    What it tests
    Seeing that a blocking send to one subscriber delays all of them, choosing a slow-consumer policy from what clients need, and sizing buffers from rates.
    First step
    Look at what Publish does when one subscriber's channel is full: if it waits, every later subscriber and every later event waits with it. Then ask what a price subscriber needs, every update or only the latest.
    Follow-ups

    From the lesson Concurrent caches, singleflight and pub/sub

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  11. Mid to senior

    A cache calls an OnEvict callback while it holds its mutex, and under load the service sometimes freezes with no error and no panic. Explain the freeze and redesign the callback path.

    What it tests
    Recognising a goroutine waiting for a mutex it already holds and slow work under a lock, and moving callbacks out of the critical section safely.
    First step
    Ask what the callback may call: if it reaches back into the cache, or waits for anything that needs the cache's lock, it waits for itself. Then consider how the cache could run its callbacks after it has unlocked.
    Follow-ups

    From the lesson Concurrent caches, singleflight and pub/sub

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes

All Go interview questions · The Go track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.