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 14

context and cancellation interview questions

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

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

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher

    What is the difference between context.Background and context.TODO?

    Answered in the lesson context: cancellation, deadlines and values

  2. Mid

    What does context.Cause return for a context ended by a plain CancelFunc?

    Answered in the lesson Cancellation causes, AfterFunc and WithoutCancel

  3. Mid

    Why should a handler pass r.Context() to db.QueryContext?

    Answered in the lesson Deadlines across a service: budgets and retries

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 nightly job ships 200,000 orders. Inside its loop it calls ctx, cancel := context.WithTimeout(parent, 5*time.Second) and defer cancel(), then ships the order. Memory climbs through the run and the job is killed near the end. Explain why, fix it, and say what go vet would and would not have caught.

    What it tests
    Knowing that deferred calls run at function exit, what a live timeout context holds, how to release each pass's context, and the limits of the lostcancel check.
    First step
    Look at when each cancel runs: defer schedules it for the end of the function, not the end of the pass, so after n orders there are n live contexts, each with a timer and a link from its parent, and n deferred calls waiting. At 200,000 orders that is 200,000 of each until the job returns.
    Follow-ups

    From the lesson context: cancellation, deadlines and values

    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

    A codebase passes the database handle, a logger, a feature-flag client and a dryRun flag through context values under string keys, so that function signatures stay short. Judge the design: what goes wrong in practice, what belongs in a context, and how you would change it.

    What it tests
    Knowing the documented scope of context values, the cost of untyped hidden dependencies, key collisions with built-in key types, and how to refactor towards explicit dependencies.
    First step
    Sort the four items by the documentation's rule: context values are for request-scoped data that crosses API boundaries, never optional parameters. The database handle, logger and flag client are shared dependencies, and dryRun changes behaviour; none belongs to one request, so all four are parameters or fields hidden in an untyped map.
    Follow-ups

    From the lesson context: cancellation, deadlines and values

    Model answer, Pro.

    In the answer:

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

    Your API logs about 4,000 "context canceled" errors an hour, and nobody can tell client disconnects from quota limits, slow dependencies or deploys. Design the change: what each component records, what the log line holds, and how alerts use it.

    What it tests
    Using WithCancelCause, WithTimeoutCause and signal causes deliberately, keeping Err checks working, logging err and cause apart, and alerting on causes rather than on a single error string.
    First step
    Start where contexts end: the middleware that enforces quotas, the calls that set timeouts, and shutdown. Make each end the context with a named cause, then log ctx.Err() and context.Cause(ctx) as two attributes. Err keeps existing checks working; the cause turns 4,000 identical lines into counts per reason.
    Follow-ups

    From the lesson Cancellation causes, AfterFunc and WithoutCancel

    Model answer, Pro.

    In the answer:

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

    A download manager streams files through io.Copy, which takes no context, into name.part files. When the user cancels, the copy must stop and the partial file must be removed exactly once, even if the download finishes at the same moment. Design it with context.AfterFunc.

    What it tests
    Using AfterFunc to interrupt a call that takes no context, reading stop's result to decide ownership, avoiding races on cleanup, and choosing what wins a tie between cancel and completion.
    First step
    Separate interrupting from cleaning up. Something has to make io.Copy return when the context ends, and exactly one piece of code must own the part file. Then decide what the result of AfterFunc's stop function tells you about which of the two endings happened.
    Follow-ups

    From the lesson Cancellation causes, AfterFunc and WithoutCancel

    Model answer, Pro.

    In the answer:

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

    After a payment succeeds, an audit record must be written even if the client disconnects. A teammate starts a goroutine that writes it with context.Background(). What is lost, how do you do it with WithoutCancel, and what stops detached writes from piling up or being cut off by a deploy?

    What it tests
    Knowing what WithoutCancel keeps and drops, adding a limit of its own, tracking detached work for shutdown, and estimating how much can be in flight when a dependency hangs.
    First step
    Compare what the two contexts carry: Background escapes the request's cancellation but drops its values, so the audit loses the request ID and trace. WithoutCancel escapes the cancellation and keeps them. Neither has a limit, so wrap it: WithTimeout(context.WithoutCancel(ctx), 2*time.Second).
    Follow-ups

    From the lesson Cancellation causes, AfterFunc and WithoutCancel

    Model answer, Pro.

    In the answer:

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

    A checkout endpoint must answer within 800 ms. It calls inventory (p99 120 ms), pricing (p99 200 ms) and payment (p99 350 ms) in sequence. Design the per-call timeouts, what happens when pricing is slow, and what the client sees in each case.

    What it tests
    Deriving per-call timeouts from the time left, keeping a reserve, refusing calls that cannot finish, and choosing what to tell the client and the callee.
    First step
    Add up the p99s first: 670 ms of the 800, so 130 ms of slack before the reserve for the response. Then give each call what is left, less the reserve, rather than a fixed share, and give each a minimum near its p99, so a call that can no longer finish is not started.
    Follow-ups

    From the lesson Deadlines across a service: budgets and retries

    Model answer, Pro.

    In the answer:

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

    A dependency was down for 30 seconds. When it came back, 5,000 clients retrying with exponential backoff without jitter knocked it over again, twice. Explain why, and design retries that let it recover, with numbers.

    What it tests
    Seeing synchronised retries, applying full jitter and caps, bounding total load with a retry budget, and keeping retries inside each request's deadline and to idempotent calls.
    First step
    Work out when the retries land if every client failed at the same moment and then waits the same 100, 200 and 400 ms, and compare each moment's load with what the service carried before it went down.
    Follow-ups

    From the lesson Deadlines across a service: budgets and retries

    Model answer, Pro.

    In the answer:

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

    A service builds its outgoing client as http.Client{Timeout: 10 * time.Second} and also passes each request's context, which often has 2 s left. Which limit ends a slow call, what error does the caller see, and which other timeouts would you still set, on the client and on the server?

    What it tests
    Knowing how the client's timeout and the request's context combine, what each one covers, how to read their errors, and which transport and server timeouts to set.
    First step
    Compare the two deadlines on each call: the context's 2 s comes first, so the context ends the slow call, whatever the client's 10 s. Then ask what each limit covers beyond that call, and what the error should tell the caller about which limit fired.
    Follow-ups

    From the lesson Deadlines across a service: budgets and retries

    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.