Go · Chapter 26 of 26

The interview

By the end of this chapter you have answered five timed questions from across the whole lab, the way a Go interview asks them: a token bucket rate limiter with the time passed in, an LRU cache that is fast and safe for goroutines, a fan-in that stops when its context ends, a goroutine leak found and fixed, and a bounded parallel map that keeps its order. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Services
  • 5 tasks
  • Go
  • In the all labs plan

The first 2 chapters of Go are free. This one comes with the all labs plan.

Why you need this

The new ingest service went live, and the evening peak was quiet. BijliGo is hiring a second backend engineer, and Joseph runs the interviews: "Short problems, about fifteen minutes each. I am not checking that you remember every function in the standard library; the docs have them. I am checking that you know what your code does inside: which goroutine waits, what is shared, who closes the channel, what happens when the context ends." Nandini liked the round so much that it is now the last chapter of the lab.

Each question is one idea from an earlier chapter, with no hand-holding: the file has the goal, the Check has the edge cases. Below is a refresher of each idea, with what Joseph listens for. The answers are not here.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1 · interview question

    Interview: ratelimit.go, a token bucket

    Time yourself: about 15 minutes. In ratelimit.go, write the type Bucket (add the fields you need), NewBucket(capacity int, every time.Duration, now time.Time) *Bucket, which returns a full bucket at now, and Allow(now time.Time) bool, which takes a token and returns true, or returns false when there is none. One token comes back every every after the last one came back, never more than capacity, and time left over between tokens is kept. Never call time.Now: the time is passed in. Bucket is safe for many goroutines.

    The Check uses capacity 5 and every 200ms from t0: 5 allowed at t0 and a 6th refused; refused at 199ms; one allowed at 200ms and a second refused; refused at 300ms; allowed at 400ms; at 900ms two allowed and a third refused; allowed at 1000ms; after a minute, 5 of 10 allowed. Then 100 goroutines call Allow at once on a bucket of 10 (every 1h): exactly 10 get true.

    Joseph will ask: why pass now in, and what does a token bucket allow that a fixed window of one minute does not?

  2. Task 2 · interview question

    Interview: lru.go, an LRU cache

    Time yourself: about 15 minutes. In lru.go, write the generic type LRU[K comparable, V any] (add the fields you need) and NewLRUK comparable, V any *LRU[K, V]; Get(key K) (V, bool) returns the value and true and marks key as the most recently used, or V's zero value and false; Put(key K, value V) stores or replaces the value, marks the key used, and drops the least recently used key when there are more than capacity; Len() int. It is safe for many goroutines.

    With capacity 3: Put C1001, C1002, C1003, Get C1001, Put C1004 drops C1002 (C1001 stays); Put C1003 again with a new value, Put C1005 drops C1004. The Check also does 100000 Puts and Gets on an LRU of 5000 and allows 2 seconds (each operation must cost about the same however full the cache is), and 50 goroutines use one LRU of 20 at once under the race detector. The standard library's container/list is a doubly linked list.

    Joseph will ask: what does each operation cost, and why do you need two data structures?

  3. Task 3 · interview question

    Interview: fanin.go, fan-in with cancellation

    Time yourself: about 15 minutes. In fanin.go, write Merge(ctx context.Context, ins ...<-chan bijli.Reading) <-chan bijli.Reading. It returns one channel that carries every reading from every input, each input's readings in their order. When every input is closed, the output closes after the last reading (with no inputs, it closes at once). When ctx ends, the output closes soon, even if the inputs never close and nobody reads the output any more. Once it has closed, no goroutine Merge started is still running.

    The Check merges three of ST001's sessions (closed inputs), merges no channels, then merges three inputs that never close, reads two readings, cancels ctx and stops reading: within 1s the number of goroutines must be back where it was, and the output must be closed.

    Joseph will ask: who closes the output channel, and how do you know every forwarding goroutine has finished?

  4. Task 4 · interview question

    Interview: leak.go, the leak

    Time yourself: about 15 minutes. Tenzin's FirstStatus(ctx context.Context, central *bijli.Central, ids []string) (bijli.Status, error) asks the central system about every charger in ids at once and returns the first status that comes back. It answers correctly, yet runtime.NumGoroutine() climbs after every call, and when every charger is down it never returns. Fix it so that it returns the first status that came back without an error; when none answers, an error that wraps the last error (errors.Is finds bijli.ErrChargerDown when they are all down); with a cancelled ctx, an error that is context.Canceled; and no goroutine it started is left behind.

    The Check calls it 20 times with C1001 to C1005 (C1003 and C1004 are down, every call takes 20ms), then with only C1003 and C1004, then with a cancelled ctx, and 1s later the number of goroutines must be back where it was.

    Joseph will ask: what is a goroutine leak, how did you find it, and what are the two ways to make sure a goroutine can always finish?

  5. Task 5 · interview question

    Interview: parallel.go, a bounded parallel map

    Time yourself: about 15 minutes. In parallel.go, write MapLimitT, R any (R, error)) ([]R, error). It calls f(ctx, item) for every item with at most limit calls at the same time and returns the results in the items' order. When a call fails it returns nil and that error, and the ctx the other calls get is cancelled so they stop early. With a ctx already cancelled it returns an error that is context.Canceled. golang.org/x/sync/errgroup is in the module cache.

    The Check runs 40 charger lookups of 20ms with a limit of 5 (40 statuses in order, at most 5 and at least 2 at once by the central system's MaxInFlight), then the same with a limit of 4 and C1004 down (bijli.ErrChargerDown, nil results, and fewer than 20 of the other lookups may still succeed), then a cancelled ctx.

    Joseph will ask: why not one goroutine for each of a million items, and what does the limit protect?

What you will learn

  • How to work a timed question: say the plan, build the smallest thing that runs, then try the edges.
  • Five ideas again, at interview depth: time passed in (chapter 14), a map with an order (6, 18 and 24), fan-in and cancellation (16, 17 and 19), goroutine leaks (15 and 16), and bounded concurrency (20).

More in Services

Build The interview yourself

BijliGo, a Bengaluru EV charging network, drops chargers at the evening peak. You rewrite its backend in Go. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Go are free. This one comes with the all labs plan.