Start With Brute Force: How to Present a Naive Solution Before You Optimize in a Coding Interview

Start With Brute Force: How to Present a Naive Solution Before You Optimize in a Coding Interview

You hear the prompt, spot a pattern, and race toward the clever answer. Two minutes later the edge cases don't fit, your complexity claim is shaky, and the interviewer is quiet. Or the opposite happens: you know a O(n) trick exists, but saying it out loud without a foundation feels like guessing.

Both failure modes share a fix. In most coding interviews, starting with a correct brute-force solution is not a downgrade. It is the fastest way to show you understand the problem, prove correctness, and give yourself a clean path to optimize under pressure.

This guide walks through when to lead with brute force, how to present it without sounding slow, and how to climb from naive to better without losing the round.

Why interviewers like a brute-force first pass

A naive solution answers questions the interviewer cares about early:

Skipping brute force often looks like confidence. When it works, you look sharp. When it fails, you have no floor to fall back to. Leading with a simple correct approach gives you that floor.

The brute-force-first loop

Use this five-step loop out loud. Keep it short; the whole pass can take three to six minutes before you start optimizing.

1. Restate the goal in one sentence

"We need every pair of indices whose values sum to target" or "We need the shortest path from start to end in an unweighted graph." A crisp restatement catches misunderstandings before you invent an algorithm.

2. State the naive approach in plain language

Say the idea before any code. "I would check every pair." "I would try every substring." "I would BFS from the start and expand level by level." If you cannot say it in one or two sentences, you do not understand it yet.

3. Sketch complexity and correctness

Name time and space roughly, and why it is correct. "Nested loops over n elements is O(n²). Every pair is considered once, so if a solution exists we find it." You do not need a formal proof. You need a clear reason the answer cannot be wrong.

4. Confirm with the interviewer before you polish

Ask: "Does this baseline make sense before I optimize?" Most interviewers say yes, or they nudge you toward a constraint you missed. Either way, you have alignment instead of coding into a void.

5. Optimize from the bottleneck you just named

Now improve the part that is slow. If the bottleneck is repeated lookups, reach for a hash map. If it is sorted order, consider two pointers or binary search. If it is exploring a graph, confirm BFS vs DFS vs Dijkstra against the weights. The optimization story should point back to the naive version: "Instead of scanning the rest of the array for the complement, I store seen values and look them up in O(1)."

What "brute force" looks like in common patterns

Two-sum style problems. Brute force: try every pair. Optimize: hash map of seen values, or two pointers if the array is sorted. The map version is easier to explain when you already showed why pairs matter.

Substring / window problems. Brute force: check every start and end index. Optimize: sliding window that grows and shrinks while tracking counts. The window only makes sense after you admit the O(n²) scan works but is too slow.

Pathfinding on a grid. Brute force: DFS exploring every path (often with pruning). Better default for shortest path on an unweighted grid: BFS. Saying DFS first, then switching to BFS for shortest path, shows you know the difference.

Sorting-adjacent problems. Brute force: generate all permutations or compare all pairs. Optimize: sort once, then scan. Sorting is a common "pay O(n log n) once" upgrade that interviewers expect you to mention.

You do not need a different personality for each pattern. Restate, naive idea, cost, confirm, then upgrade.

How to sound fast while starting slow

Candidates worry that brute force wastes the clock. The opposite is usually true: a two-minute naive sketch saves the ten minutes you would burn debugging a half-remembered trick.

A few phrasing habits help:

Common mistakes

Jumping to the trick with no baseline. If the trick fails, you have nothing to fall back on and no story for why the next idea is better.

Implementing the full O(n²) solution when time is tight. Describe it, confirm, then code the optimized version unless the interviewer asks for both.

Calling something brute force when it is already wrong. A wrong nested loop is not a baseline. Correctness first; speed second.

Optimizing before you know the constraint that matters. Ask about n, value ranges, and whether the input is sorted. Sometimes O(n²) is fine for n ≤ 100, and the interviewer is testing judgment, not cleverness.

Going silent while you search for the optimal. Say you are starting naive on purpose. Silence reads as stuck; a labeled plan reads as control.

A short worked example

Prompt: return true if any two numbers in the array sum to target.

  1. Restate: "Find whether any pair adds to target."
  2. Naive: "Check every pair with two loops."
  3. Cost: "O(n²) time, O(1) extra space. Correct because every pair is examined."
  4. Confirm: "I'll use that as the baseline, then improve lookups."
  5. Optimize: "As I scan left to right, store each value in a set. For each new value x, check whether target - x is already in the set. That is O(n) time and O(n) space."

You can implement the hash-set version immediately after that speech. The interviewer already heard that you know the slower correct answer and why the faster one works.

How to practice the habit

  1. On every practice problem, force a thirty-second brute-force statement before you reach for patterns.
  2. Write the complexity of the naive version first, then the optimized one, as a two-line note.
  3. Practice saying the upgrade as a diff out loud, not only thinking it.
  4. Timeboxed mocks help. Give yourself two minutes for the baseline story, then ten for the improved code. That mirrors real rounds better than silent LeetCode grinding.
  5. Review recordings or notes. If you skipped the baseline and got stuck, mark it. The pattern is trainable.

If you want structured prompts beside your editor while you rehearse this loop in mock rounds, Luna Interview is a Chrome side-panel interview practice copilot that keeps the problem, your plan, and follow-ups in view: https://lunainterview.xyz/

FAQ

Won't I look weak if I start with O(n²)? Not if you label it as a baseline and move on. Interviewers usually prefer a correct slow answer plus a clear upgrade over a broken clever attempt.

What if I already see the optimal solution? Say so briefly: "I think a hash map gives O(n); I'll still name the pairwise check as the baseline, then implement the map version." You get credit for both.

What if the interviewer says to skip brute force? Skip it. The loop is a default, not a rule. Follow their lead.

How long should the naive pass take? Usually under three minutes of talking, sometimes less. If you are still coding the full naive solution at minute eight, you stayed too long.

Does this apply to system design? Related idea, different shape. In design rounds you still start with a simple working architecture before scaling it. In coding rounds, keep the baseline tied to the exact problem on the pad.

Starting with brute force is not playing small. It is how you stay correct, stay calm, and give yourself a clean ladder to the solution the interviewer actually wants to see.