Code Review and Working with Existing Codebases Questions
Reviewing others' code and navigating unfamiliar systems: giving and receiving actionable review feedback, spotting correctness and design issues, and reading and understanding large or legacy codebases before changing them. Covers collaborative coding norms, incremental change in shared repositories, and verifying changes against existing behavior. The team-facing side of day-to-day engineering.
List five elements you always include in a pull request description to make reviews efficient for team members and new joiners. Explain why each element matters for maintainability, discovery, and onboarding.
Sample Answer
Direct answer
A good pull request (PR, a proposed code change submitted for review) description answers what changed, why, how it was tested, what the reviewer should focus on, and what's deliberately out of scope. Each element saves a reviewer from reconstructing context from the diff alone, and later saves an engineer doing archaeology on why the code looks the way it does.
Structured elaboration
Five elements and why each matters
- What changed (a one-to-two sentence summary): gives the reviewer a frame before reading the diff line by line; without it they infer intent from code, which is slower and more error-prone
- Why (the motivating problem, with a linked ticket or incident if there is one): this is the piece that survives long after the code changes again; an engineer later using
git blameor the commit log to understand "why is this here" finds the why in the description, not the diff - How it was tested: tells the reviewer what confidence already exists (unit tests added, manually verified in staging), so they know what's already covered versus what still needs scrutiny
- What to focus review on: the author usually knows which part of the diff is riskiest or most novel; naming it directs a time-constrained reviewer's attention where it matters most
- What's explicitly out of scope: prevents scope-creep comments ("why didn't you also fix X") and tells a future reader that a related-looking issue was a deliberate non-goal, not an oversight
Why this matters beyond the immediate review
- Maintainability: the description becomes permanent documentation attached to the commit history; months later it's often the only record of intent
- Discovery: someone tracing "why does this function do X" through the commit log finds the description as the answer, faster than re-deriving it from the diff
- Onboarding: a new engineer reading recent PR history to understand a codebase's evolution gets a narrated version of the system's history instead of bare diffs
Worked example
A PR titled "Fix race condition in session refresh" with the description "fixes bug, see diff" forces the reviewer to reverse-engineer what race condition, why this fix addresses it, and whether it was actually reproduced. Contrast: "What: adds a lock around session-token refresh. Why: intermittent authentication failures under concurrent requests, traced to two requests refreshing the same token at once and one overwriting the other's write. Tested: added a test that fires 50 concurrent refresh calls and asserts exactly one network call happens; manually reproduced the original bug on staging before the fix and confirmed it no longer reproduces after. Focus review on: the lock's scope, it needs to cover the full read-modify-write, not just the write. Out of scope: the underlying token storage isn't thread-safe either, that's a separate, larger fix tracked elsewhere." A reviewer can now scrutinize exactly the risky part (the lock's scope) instead of re-deriving the whole problem, and anyone reading this PR later understands both the bug and the deliberate boundary around the fix.
Trade-offs and pitfalls
- A template that's too long or bureaucratic gets filled in with boilerplate nobody reads; keep it to the elements that actually change reviewer behavior
- A "why" section that just restates the ticket title without the actual reasoning doesn't help; the value is in the reasoning, not a ticket number
- Descriptions get stale if the PR's scope changes mid-review and nobody updates them; treat the description as something to revisit before merge, not only before opening the PR
Advanced technical domain: A long-running monitoring agent has a memory leak in production. As a reviewer of the agent's codebase, describe the steps you would take to identify leaking code during review: which profilers and CI checks to add, which code patterns to look for (circular refs, global caches, goroutine leaks), and what automated tests or metrics would catch regressions early.
Sample Answer
Direct answer
I would not try to find a production memory leak purely by reading code. I'd combine dynamic evidence, actually profiling the running agent under load, with a targeted code-review checklist for the patterns most likely to cause a slow, steady leak, and then lock in whatever I find with a CI check so the same class of bug can't silently come back.
Structured elaboration
Profilers and dynamic evidence. Before trusting any code-review guess, I'd reproduce the leak under a controlled, repeatable workload and watch memory over time: resident set size (RSS, the actual physical memory the process holds) climbing steadily and never coming back down after garbage collection runs is the signature of a real leak, as opposed to a memory spike that a full garbage collection cycle reclaims. For a compiled agent, a built-in heap profiler (Go's pprof, for example) can take a heap snapshot before and after a workload and show exactly which allocations are still retained; for an interpreted agent, an allocation tracer (Python's tracemalloc, for example) does the equivalent by snapshotting live allocations and diffing two points in time.
Code patterns to look for. The question names circular references specifically, and it's worth being precise about when that's actually the cause: in a language with a tracing garbage collector (which reclaims anything unreachable from a root, cycles included), a circular reference on its own is usually NOT what leaks memory, since the collector can free a cycle nobody outside it points to. What actually leaks in that kind of runtime is something still reachable that shouldn't be:
- Unbounded global caches or maps: a cache or dictionary that only ever grows, with no eviction policy or size cap, is one of the most common leak sources in a long-running service, since every entry is reachable from a live root for the life of the process.
- Leaked lightweight concurrent tasks (goroutines, in Go, or the equivalent worker/coroutine in another runtime): a task that's spawned per request or per event but never exits, most often because it's blocked forever waiting on a channel or queue that nothing will ever write to, keeps itself and everything it captured in its closure alive indefinitely. Every hung task is a small, permanent leak.
- Forgotten deregistration: an event listener, callback, or subscription that's added but never removed when the thing that registered it goes away, so a long-lived object (say, a connection pool) keeps growing a list of listeners that should have been cleaned up.
- Circular references DO matter directly in a reference-counted runtime (no tracing collector, or one that only handles simple cases), where two objects each holding a strong reference to the other can prevent the count from ever reaching zero; if the agent embeds any component like that, it's worth checking specifically.
CI checks and metrics that catch regressions early.
- A memory-regression test: run the agent against a fixed, deterministic synthetic workload (a set number of requests or events, not "for N minutes," so the result is reproducible), take a heap snapshot before and after, and fail the build if retained memory grows past a set threshold.
- A task-leak assertion: after the synthetic workload finishes and drains, assert that the number of live background tasks (goroutines, threads, whatever the runtime calls them) has returned to its known baseline count, not just "isn't growing forever."
- Production metrics and alerting: track RSS trend, heap object count, and live background-task count over time, and alert on sustained upward trend rather than a single spike, since a spike that recovers after garbage collection is expected behavior, not a leak.
Worked example
A concrete pattern this would catch: the monitoring agent spawns one background task per incoming metric batch to forward it to an upstream collector over HTTP, and that HTTP call has no timeout or deadline. If the upstream collector stops responding, every task blocked on that call never returns, and each one keeps its metric-batch buffer alive in its closure. Under normal conditions this is invisible, since tasks come and go quickly; the moment the upstream collector gets slow or unresponsive, the agent starts accumulating one leaked task (and its buffer) per batch, forever, which shows up as a slow, steady RSS climb that never plateaus.
The review-time catch: does every code path that spawns a background task per request or event pass it a context or deadline, so a hung downstream call can't block that task forever? The CI catch: a synthetic test that sends, say, 500 metric batches to a fake upstream collector that never responds, then asserts the live background-task count returns to its pre-test baseline (not zero, since some fixed background workers are expected) once the test workload finishes, rather than staying elevated by roughly 500.
Trade-offs and pitfalls
Continuous production profiling has real overhead, so it's usually run on-demand or sampled, not left on all the time. A CI memory-regression gate with an absolute byte threshold is prone to flaking across different CI runner hardware; a relative threshold (percent growth over baseline, measured on the same run) is more stable. The most common wrong turn is fixing the symptom instead of the cause: bounding a growing cache with a size limit stops the crash but, if entries are evicted on a schedule that doesn't match how they're actually used, can just delay the same leak rather than fix it, so the review should ask why the cache grows unbounded in the first place, not just cap it and move on.
You observe several flaky tests in CI that pass locally but fail intermittently on the pipeline. As a reviewer and engineer, walk through your triage and remediation plan, and how you'd communicate status to the team while it's unresolved.
Sample Answer
Direct answer
Don't quarantine and forget. Flaky tests, tests that pass locally but fail intermittently in CI (continuous integration), erode trust in the whole suite fast. Confirm it's actually flaky rather than a real intermittent bug, isolate the likely cause, fix or explicitly quarantine it with a tracked owner, and keep the team visibly informed the whole time so people don't start treating red CI as normal.
Structured elaboration
Confirm it's flaky, not a real bug: re-run the specific test many times in isolation, and also alongside its usual neighbors. If it only fails in combination with other tests, that's a strong clue it isn't really about the test itself.
Common causes, checked in order:
- Timing or async: does it wait on a real signal that an async operation finished, or on a fixed sleep?
- Shared or leaked state: does it depend on data left behind by another test, or share a global or database row with a test running in parallel?
- Test order dependency: does it pass in isolation but fail only after a specific other test runs first?
- External dependency: does it hit a real network call, the system clock, or unseeded randomness?
Remediate: fix the root cause where possible, mock the external dependency, isolate test data, replace a sleep with waiting on the actual condition. If it can't be fixed immediately, quarantine it explicitly, skip it with a linked ticket and a named owner, rather than leaving it red and training people to ignore CI failures.
Communicate status: post in the team channel when a test is quarantined and why, keep a visible list of currently-quarantined tests so it doesn't become an invisible, growing pile, and set an expectation for when it'll be revisited, since "quarantined forever" behaves the same as deleted, but worse, because it still looks like coverage exists.
Worked example
A test for an async file-upload flow fails about 1 in 20 runs in CI but never locally. Re-running it 30 times in isolation, it passes every time. Re-running the full suite, it fails only when run after a specific test that doesn't clean up its own temp file. Root cause: the earlier test's leftover file makes the upload test see unexpected existing state. Fix: add proper teardown to the earlier test, and switch the flaky test's own wait off a fixed sleep and onto the actual upload-complete signal, reducing remaining timing risk too. Communication: a short note in the team channel when it's identified as flaky, not yet a merge blocker, and a follow-up once it's confirmed fixed.
Trade-offs and pitfalls
Automatically retrying a flaky test until it passes hides the problem instead of fixing it, and the underlying issue can silently get worse over time. Blocking all merges on every flaky failure without triage first punishes the whole team for one bad test and trains people to distrust CI. The right balance is quarantine with a named owner and a deadline, not indefinite silence in either direction.
You are reviewing a change that reduces test coverage from 85% to 68% but the author argues the removed tests were flaky and unreliable. As a reviewer, outline a principled decision process: what evidence to request, how to measure test value, alternatives to removing tests, and how to document the decision so future reviewers can understand the risk.
Sample Answer
Direct answer
Don't accept "the tests were flaky" as self-certifying. Treat a large coverage drop as a risk decision that needs evidence: ask for the flakiness data, look for a way to keep the safety net while removing the noise (fix, quarantine, or narrow the test before deleting it), and if deletion really is the right call, document the risk and the reasoning so a reviewer looking at this months later can see what trade-off was made and why.
Structured elaboration
Evidence to request. Continuous integration (CI, the automated pipeline that builds and runs tests on every change) history for the removed tests over recent weeks, showing pass/fail per run; failure logs for a few representative failures; any links to prior attempts to fix them; and a plain description of what behavior each removed test actually asserted, since "flaky" and "not worth having" are two different claims that both need their own evidence.
How to measure test value. A useful starting point is a flakiness rate: how often a test fails non-deterministically, out of how many runs, over a recent window. Teams commonly treat anything in the low single digits as worth investigating and anything much higher as a real problem, but that's a tunable convention to calibrate to your own pipeline, not a fixed rule. Weigh that flakiness rate against the test's actual defect-detection value: does it cover a code path that has broken in production before, or a genuinely critical invariant, versus an incidental implementation detail that happens to be covered along the way?
Alternatives to removing tests. Fix the root cause (replace a real sleep-based wait with an explicit event or a fake clock, mock a flaky network call, isolate shared test state between runs); quarantine the test into a separate, still-running "known flaky" suite that's tracked but doesn't block merges; add a bounded, justified retry for a genuinely transient cause; or narrow the test so only the brittle assertion is removed, keeping the rest of the coverage.
Documenting the decision. Put the evidence, the alternatives considered, and the reasoning for why deletion won (or why a different fix was chosen) into the PR (pull request) description or a linked ticket, with a named owner and, for anything quarantined rather than truly resolved, a re-evaluation date. A future reviewer looking at the coverage graph should be able to find out why it dropped and whether that was a considered decision or a silent regression.
Worked example
An illustrative review comment applying this process to the scenario in the question:
"This PR removes 4 tests and drops coverage by a double-digit number of percentage points. Before I approve: can you attach the CI history showing these 4 failing intermittently over the last several weeks, and confirm whether you tried at least one stabilization approach, for example replacing a sleep-based wait with an explicit event, before deciding to delete them? If they're genuinely not fixable in a reasonable time, I'd rather see them moved into a quarantined suite with a tracking ticket and an owner than deleted outright, so we don't lose the safety net for the code path they cover. Either way, please add a short note to the PR description with the evidence and the decision, so this is traceable later."
Trade-offs and pitfalls
Quarantining without real follow-through is functionally the same as deletion but looks safer on paper, so a quarantine ticket needs a real owner and a real re-evaluation date, not just a label that never gets revisited. Sometimes a test genuinely is asserting an implementation detail rather than behavior, and removing it is a real improvement, so don't reflexively block every coverage drop, the point is evidence, not a floor on the percentage. The biggest pitfall is judging purely by the coverage number: two tests covering the same trivial line count the same toward that percentage as one test covering a critical path that has actually broken production before, so the review has to look at what was removed, not just how much.
Write a Python script that parses the output of git diff --numstat (tab-separated added, deleted, filename lines) from STDIN and emits JSON with total additions, total deletions, number of files changed, and a list of files exceeding 200 additions. Describe how this script could be integrated into a CI job to label large PRs automatically.
Sample Answer
Direct answer
Below is a script that reads git diff --numstat from STDIN (standard input, the default input stream a program reads from when data is piped into it) and emits one JSON object with total additions, total deletions, files changed, and any files whose additions exceed 200. Wiring it into continuous integration (CI, the automated build/test pipeline that runs on every push) means piping the pull request (PR)'s diff into the script and using its output to apply a label through the CI platform's API.
Structured elaboration
Approach
git diff --numstatemits one tab-separated line per changed file:<added>\t<deleted>\t<filename>- Binary files report
-for added and deleted instead of a number, this must be handled explicitly rather than crashing the parser - Parse each line, accumulate totals, and separately collect filenames whose additions exceed the 200-line threshold
Code
import sys
import json
def parse_numstat(lines):
total_additions = 0
total_deletions = 0
files_changed = 0
large_files = []
for line in lines:
line = line.rstrip("\n")
if not line.strip():
continue
parts = line.split("\t")
if len(parts) != 3:
continue
added_str, deleted_str, filename = parts
files_changed += 1
# binary files report '-' for added/deleted; treat as 0 rather than crash
added = int(added_str) if added_str != "-" else 0
deleted = int(deleted_str) if deleted_str != "-" else 0
total_additions += added
total_deletions += deleted
if added > 200:
large_files.append(filename)
return {
"total_additions": total_additions,
"total_deletions": total_deletions,
"files_changed": files_changed,
"large_files": large_files,
}
def main():
lines = sys.stdin.readlines()
result = parse_numstat(lines)
print(json.dumps(result))
if __name__ == "__main__":
main()
CI integration
- In CI, compute the numstat against the PR's actual merge base (for example
git diff --numstat origin/main...HEAD, or the CI platform's own base-comparison ref) and pipe it into the script - Parse the JSON output and use a non-empty
large_fileslist, ortotal_additionscrossing a team-chosen threshold, to apply a "large-pr" label through the CI platform's API (for example the GitHub CLI'sgh pr edit --add-label, or a direct REST call) - Run this as an early, cheap CI step, before slower checks, so the label is visible to reviewers as soon as the PR opens
Worked example
I ran this exact script against the following sample git diff --numstat input:
5 2 src/app.py
0 0 README.md
250 10 src/big_module.py
- - assets/logo.png
Piped through the script (... | python3 diffstats.py), the actual output was:
{"total_additions": 255, "total_deletions": 12, "files_changed": 4, "large_files": ["src/big_module.py"]}
That matches the arithmetic by hand: additions 5 + 0 + 250 + 0(binary) = 255, deletions 2 + 0 + 10 + 0(binary) = 12, four lines means four files changed, and only src/big_module.py has additions (250) over the 200 threshold.
Trade-offs and pitfalls
numstatreports-for binary files; treating that as 0 additions is a deliberate choice so the parser doesn't crash, but it means a genuinely large binary change (a big generated asset) won't trip the large-file flag- A pure line-count threshold doesn't capture every sense of "large": 250 lines of an auto-generated config file is not the same risk as 250 lines of hand-written logic; use this as a first-pass signal, not the only one
- Commits within the PR don't matter to this script, since it's comparing the merge base to the tip, which is usually what you want for a PR-level label
- If the base ref is stale (a local branch that hasn't been rebased), the numbers will be wrong; make sure CI diffs against the PR's true merge base, not a stale local reference
Unlock Full Question Bank
Get access to all Code Review and Working with Existing Codebases interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.