Guide · coding-prep
How to Write Clean Code Under Time Pressure in an Interview
Optimize for legibility, not elegance. Use boring names, small functions, one idea per line, and skip clever tricks. The interviewer is grading whether you write code a teammate could review on Monday — not whether you can golf 12 lines into 3.
By Alex Chen, Founder, InterviewChamp.AI · Last updated
How do you write clean code under time pressure in a coding interview?
Optimize for legibility. Use boring, descriptive names. Pull helpers out when the main function gets dense. Keep one idea per line. Skip clever one-liners. The grade is whether a teammate could review your code on Monday — not whether you compressed twelve lines into three. Clean is faster than clever, and faster reads as more confident.
The five highest-leverage clean-code moves
These are the moves that take seconds and visibly raise your code quality:
- Name variables for what they hold, not what they are.
seenbeatsset1.merged_intervalsbeatsresult.customer_countbeatsn. - Use early returns to flatten nesting.
if not nums: return 0at the top is much cleaner than wrapping the rest of the function inelse. Three levels of nesting is usually one early-return short of two levels. - Extract a helper when the inline logic needs a comment. If you'd need to comment "this checks if the move is legal," extract
is_legal_move(state, move)instead. - One idea per line. Don't combine assignment, arithmetic, and a function call on one line just to save vertical space. The interviewer reads top-to-bottom; dense lines slow them down.
- Use language idioms.
enumerate()instead ofrange(len(...)). List/dict comprehensions for simple maps and filters.collections.Counterfor frequency counting. Idioms read as fluent.
The Pragmatic Engineer's writing on code review signal consistently finds that engineers grade unfamiliar code on "can I follow this in one read." Interview code is reviewed under the same instinct — they're reading it once, often while you're still finishing.
Naming under pressure
Under time pressure, most candidates default to single letters and have to rename later (or never). Build the habit of writing real names from the start.
A simple naming menu for the common interview structures:
- Counters / accumulators:
total,result,running_sum,max_length— verb-object or quality-noun, neveransorx. - Containers: Plural of what they hold.
seenfor a set of seen things,frequenciesfor a Counter,mergedfor the output list. - Indices into a structure:
i,jare okay only inside a tight 1-3 line scope. Past that,left,right,slow,fast,start,endare better. - Helper inputs: Match the domain.
is_valid_move(board, move)notf(b, m).
The cost of typing merged_intervals instead of r is about half a second. The cost of forgetting what r meant on line 22 is much larger.
When to extract helpers
The trigger: when your main function gets past 15-20 lines OR contains a logical subtask with a clear name.
# Cluttered — everything in one function:
def schedule(meetings):
meetings.sort(key=lambda m: m[0])
result = []
for m in meetings:
if result and m[0] <= result[-1][1]:
result[-1] = (result[-1][0], max(result[-1][1], m[1]))
else:
result.append(m)
return result
# Cleaner — the merge logic gets a name:
def schedule(meetings):
meetings.sort(key=lambda m: m[0])
merged = []
for m in meetings:
if merged and overlaps(merged[-1], m):
merged[-1] = merge(merged[-1], m)
else:
merged.append(m)
return merged
Extracting two tiny helpers — overlaps() and merge() — turned an 8-line function into a 6-line one with three named operations. The interviewer can now read the main function once and understand the algorithm without parsing tuple-indexing math.
The comment budget
Most candidates either over-comment (every line) or under-comment (zero). The right answer is 1-3 short comments per medium-sized solution, placed at decision points:
- "Sort ascending — we need to process the smallest unfinished task first."
- "Two-pointer instead of nested loops because the array is sorted."
- "Early return for the empty case to avoid index errors below."
Per Indeed Career Guide research on technical-interview rubrics, interviewers weight "code quality" as roughly 25-30% of the overall coding-round grade — and "purposeful comments at decision points" appears as a frequent positive marker. Decorative comments (# loop through the array) do not.
What to refactor if you have time left
After your solution works and you've stated complexity, if you have 2-4 minutes left, refactor out loud: rename one or two ambiguous variables ("I'm renaming temp to prev_max since that's what it actually holds"), extract one helper if a chunk has a clear name, add one comment at the trickiest decision point, then restate the complexity once more. This refactor pass is one of the most underrated interview moves — it signals you take code quality seriously even when the clock is running, which is a strong proxy for how you'd behave on a real team. Candidates who sit silently when the clock has time left almost always score lower than those who use it.
About the author: Alex Chen is the founder of InterviewChamp.AI and writes about the modern tech interview from the inside — what changed, what works for new grads, and where the old playbook fails.
Frequently asked questions
- Should I extract helper functions in an interview?
- Yes, once your main function gets past ~15 lines or has a clearly-named subtask. Extracting `is_valid_move(state)` or `merge_intervals(top, new)` makes the main logic readable in one glance, which is one of the highest-leverage clean-code moves you can make under time pressure.
- Are one-letter variable names ever okay?
- Only for loop counters in 1-2 line scopes and standard math vars like x, y for coordinates. Beyond that, use real names — i, j, k inside a 30-line function is what makes code hard to read. The cost of typing four extra characters is zero; the cost of unclear code is large.
- Is it okay to use a clever one-liner if it solves the problem?
- Only if the one-liner is idiomatic and the interviewer would immediately recognize it. A list comprehension with two conditions is fine. A nested ternary or a chained reduce is not — readability trumps brevity, every time, in interview code.
- Should I add comments?
- Add 1-2 comments at decision points (where you chose one structure over another) and skip the rest. Don't comment what the code obviously does. The narration you do out loud is already the running commentary — written comments should mark intent, not echo syntax.
- What if my solution is ugly but works — should I refactor?
- Yes, if there's 3+ minutes left. Refactoring under time pressure shows judgment and is itself graded. Extract two helpers, rename three variables, and re-state the time complexity. That's often the difference between a 'lean hire' and a 'strong hire'.