Sub topic 13
Explaining Your Approach in Reviews and Interviews
This chapter assumes you have already written a brute-force solution, estimated its cost against the input limits, and seen at least one way to optimize it. The focus here is on talking about that work: how to present a brute-force-first approach so that an interviewer or a reviewer can follow your reasoning and trust your result.
Why the Explanation Matters
A brute-force solution with no explanation can look like you never thought of anything better. The same code, presented with a short account of its search space and cost, shows that you can reason about correctness and performance as separate steps. Interviewers usually score the reasoning as well as the final code. Reviewers want to know whether the slow version is intentional, whether it is fast enough, and whether it can be replaced safely.
A Repeatable Script
The same order of steps works in both settings. Each step answers a question the listener is likely to have.
- Restate the problem. Confirm the inputs, the outputs, and edge cases such as empty input, duplicate values, and the case where no answer exists.
- State the search space. For example: "Every pair of distinct indices i < j. There are n(n-1)/2 of them."
- Give the cost. "That is O(n^2) pair checks."
- Check it against the limits. "With n up to 10^5, that is about 5 × 10^9 checks, which is too slow."
- Name the redundant work. "For each j, the inner loop scans every earlier value again just to find one specific number, target - nums[j]."
- Propose the fix and its new cost. "Keep the earlier values in a dictionary. Each lookup takes expected O(1) time, so the whole pass is O(n)."
- Verify. "I'll check the fast version against the brute force on small random inputs."
Writing It Down for Code Review
In a pull request, the explanation belongs in the code and in the description. When brute force is the version you ship, a docstring stops a reviewer from "optimizing" it without a reason.
def find_conflicts(meetings):
"""Return every pair of overlapping meetings.
Brute force on purpose: this checks all n*(n-1)/2 pairs, O(n^2).
A user's calendar holds at most 200 meetings per day, so that is
under 20,000 checks. A sort-and-sweep version would be faster but
harder to read, and the output can contain O(n^2) pairs anyway.
"""
conflicts = []
for i in range(len(meetings)):
for j in range(i + 1, len(meetings)):
(s1, e1), (s2, e2) = meetings[i], meetings[j]
if s1 < e2 and s2 < e1:
conflicts.append((i, j))
return conflicts
print(find_conflicts([(9, 10), (9.5, 11), (12, 13)])) # [(0, 1)]When you replace a brute-force solution, keep the old version in the test suite as the oracle and mention it in the PR description: "The new version matches find_conflicts_brute on 10,000 random calendars."
Common Mistakes
| Mistake | What it signals | Better phrasing |
|---|---|---|
| Coding the fast version without mentioning a baseline | You may have memorized the answer | "The obvious approach is X at O(n^2); here is why it's too slow." |
| Apologizing for brute force | You see it as failure | "This is correct and O(2^n). With n ≤ 20, that's about 10^6 subsets, which is fine." |
| Giving a cost with no input size | Nothing to check the claim against | Always pair the Big-O with the actual limit. |
| Spending too long on the brute force | Poor use of the time | Describe it in two or three sentences, and write it out in full only if asked. |
| Claiming the optimization is correct without a check | Untested assumption | Offer a stress test or trace a small example by hand. |
Exercises
Exercise
An interviewer asks for the shortest tour through n cities, with n ≤ 10. Write the two or three sentences you would say before coding.
Show answer
The search space is every ordering of the cities after fixing city 0 as the start, which gives (n-1)! tours. With n = 10 that is 9! = 362,880 tours, each costing about 10 additions, so roughly 4 × 10^6 operations. That fits easily, so I'll enumerate the permutations directly and keep the minimum.
Exercise
A reviewer comments "This is O(n^2), please optimize" on a function whose input never exceeds 50 items. How do you respond?
Show answer
Acknowledge the cost and give the numbers: at most 50 × 49 / 2 = 1,225 checks, which is negligible. Explain that the simple version is easier to verify, and add a docstring stating the bound so the decision is recorded. Offer to optimize if the bound on the input is ever expected to grow.