Logo

CodeKerdos

Login

How to Think in DSA: From Brute Force to Optimal

If you’ve solved hundreds of DSA problems, you know this feeling: you sit in an interview, a new question appears on the screen, and within seconds your confidence drops.

“I’ve never seen this before.”

Let me say this clearly - that reaction is normal. Even strong candidates face it. The difference is not in how many problems you’ve solved. It’s in how you respond in that moment.

This blog isn’t about another list of patterns. It’s about how to think when the problem is unfamiliar - how to move from brute force to optimal in a structured, calm way.

blog bg

The Real Goal of DSA in Interviews

Interviewers are not checking whether you’ve memorized a solution. They’re checking whether you can:

  • Understand a problem clearly

  • Build a working approach

  • Analyze its efficiency

  • Improve it logically

That’s it.

If you approach DSA as a thinking exercise instead of a memory contest, everything changes.

Start With Clarity, Not Code

When you see a problem, don’t rush into writing code. Slow down.

Understand: What exactly is being asked? What does the output represent? What are the constraints? Are there edge cases?

Many mistakes happen because candidates start coding before fully understanding the problem. In interviews, clarity is more impressive than speed. Once you understand the problem, your next move should almost always be the same.


Build the Brute Force First

This is where most people hesitate. They think brute force will make them look weak. In reality, it shows structured thinking. Brute force means: solve the problem in the most straightforward way possible, even if it’s inefficient.

Why start here? Because brute force:

  • Proves you understand the logic
  • Gives you a correct baseline
  • Makes optimization easier

You can’t optimize what you don’t understand. Once you have a brute force idea, the next step is critical — analyze it.

Think in Terms of Time Complexity

After defining your approach, ask: how fast is this?

Time complexity measures how the running time grows as input size increases.

If your solution uses:

  • One loop → likely O(n)
  • Two nested loops → likely O(n²)
  • Three nested loops → likely O(n³)

Now connect this to constraints.

If the input size can be 100,000, then:

  • O(n²) might be too slow
  • O(n³) is definitely too slow
  • O(n) or O(n log n) is ideal

In interviews, explicitly saying: “This approach is O(n²). Given the constraints, it may not scale well. Let me improve it.” shows maturity and awareness. That’s strong engineering thinking.

Then Think About Space

Space complexity is about how much extra memory your solution uses.

  • If you only use a few variables, that’s constant space O(1).
  • If you use an extra array of size n, that’s O(n).
  • If you build a 2D structure of size n × n, that’s O(n²).

Optimization is not just about making things faster. It’s about balancing time and space. Sometimes you can reduce time by using extra memory. Sometimes you reduce memory but increase computation. There is no universally perfect solution — there is only the best solution under given constraints.Understanding this tradeoff is what separates someone who “codes” from someone who engineers solutions.

Identify the Bottleneck

Now comes the real optimization step. Look at your brute force and ask:

Most optimizations come from removing repetition or restructuring logic. For example:

Optimization is rarely magic. It’s systematic reduction of inefficiency.


Pattern Recognition (Without Memorization)

Over time, you’ll notice that many problems share structural similarities. Not exact statements — but similar shapes.

Some problems revolve around scanning arrays efficiently. Some involve making optimal decisions step by step. Some involve exploring possibilities recursively. Some depend heavily on constraints and ordering.

Instead of memorizing solutions, train your brain to recognize structure.

When you see a new question, ask:This mindset makes unfamiliar problems less scary.

What Usually Goes Wrong in Interviews

From experience, the most common issue isn’t lack of knowledge. It’s panic.
You might have solved 200 problems. But when a new one appears, your brain searches for a direct match. When it doesn’t find one, it freezes. Instead, rely on process:

Improved Problem-Solving AbilityYou learn to:
  1. Understand the problem clearly.
  2. State a brute force approach.
  3. Analyze time and space complexity.
  4. Identify inefficiencies.
  5. Improve step by step.
Even if you don’t reach the most optimal solution, structured thinking leaves a strong impression. Interviewers are evaluating reasoning more than perfection.

If you want to truly get comfortable with this way of thinking, don’t just solve random problems. Study recurring problem structures intentionally. Learn how different categories of problems are optimized and why certain approaches work better under certain constraints.

Master the thinking process. The optimal solution will follow.