DEV Community

Anita Olsen
Anita Olsen Subscriber

Posted on • Edited on

What is Your Approach to Solving Problems With Code?

I do my best to remember what I have learned, to think outside the box, to be creative and break the code into smaller parts when necessary, but it is not always easy to do so.

CodeCombat sometimes asks me to complete a level with x line of code. For example: "Use 8 lines of code" and another which offers a greater reward with "Use 4 lines of code". I usually manage to complete the level with the lowest maximum line of code and sometimes even less than that which makes me feel great!

Problem-solving is one of the most important skills a programmer should have as programming is about problem-solving as well as writing lines of code. There is more than one way to solve problems and I wonder what other people's approaches to problem solving are.

Do you have a particular approach to problem solving? Do you have a step method or something which you follow? What is your approach to solving problems with code?

Top comments (33)

Collapse
 
ashiqsultan profile image
Mohamed Ashiq Sultan • Edited

This may not work for coding challenges and Im not sure why number of lines matter. But below is what I follow for real time problems

  1. Never start to solve the problem directly from code.
  2. Have a pen and paper, visualize what exactly you are trying to solve. Think twice, code once
Collapse
 
ben profile image
Ben Halpern

A key for me is reduction of variables. So like removing lines of code debugging etc.

I think this applies to general problem solving. Figure out what you don't have to worry about first.

Collapse
 
csm18 profile image
csm

A good teacher on youtube once said:

  1. Understand the problem
  2. Think of a solution
  3. Implement that solution with brute force method. That is, just get a working prototype.
  4. Then look at your code again, see where you can optimise.

This approach really worked for me!

Collapse
 
mohitdecodes profile image
Mohit Decodes

Right

Collapse
 
linkbenjamin profile image
Ben Link

I tend to find that if a Developer is going to go "off the reservation" they'll do it in chasing that bit of optimization that gets us from 6 lines to 5 or whatever... so I try to stay focused on the words "Solving Problems" rather than "with Code".

First iteration: Solve the problem.

Second: Clean up egregious stuff.

Try to stop there unless new information comes to light or unless the system is life-and-death - because otherwise, you can spend a lifetime tweaking something - the returns diminish rapidly.

Collapse
 
thesyntaxdude profile image
pope βœͺ

I have learnt the hard way that diving headfirst into code is a bad idea.
My process is simple.

  1. Understanding the problem - You can research with google or AI, YOUR CHOICE.
  2. Plan the solution -You could use diagrams or just write out the steps you think are needed in plain English.
  3. Turn your plan into something that looks like code but with 0 syntax errors, pseudo code.
  4. From there, you are free to use any programming language you want to build whatever it is you are building.

This reduces time spent figuring out how stuff work when writing the actual code.

Collapse
 
pengeszikra profile image
Peter Vivo • Edited

My first step to solve a problem, is setup the state structure which collect a necessary data from input and good enough to represent the goal data.
After that step I just need to make two method:

  • convert input to state.
  • find out the result by use state

This method work even complex problem, when state are some stream.

Collapse
 
ankit_rattan profile image
Ankit Rattan

I usually start by breaking the problem into smaller, manageable parts to clearly understand what’s being asked. Once I’ve grasped the requirements, I try to relate it to any standard approach or technique I’ve learnedβ€”either from coursework or practice. I actively solve daily problems, like LeetCode’s Problem of the Day (POTD), and regularly follow structured problem-solving sheets like Striver’s, which also include detailed solution approaches. This routine helps me recognize patterns more quickly and apply the most efficient solutions. Hope this works!
Just break the problem into smaller problem -- (just like tagline of recursion πŸ˜…)

Collapse
 
deividas_strole profile image
Deividas Strole

My approach to solving problems with code starts with understanding the whyβ€”what exactly needs to be solved and who it impacts. I break the problem down into smaller, manageable parts, then plan a solution before touching any code. I usually write pseudocode or draw diagrams to map things out, then start building iterativelyβ€”testing and refining as I go. I also try to keep things simple and readable, because future me (or someone else) will thank me later. And when I get stuck, I ask questions, read docs, or look for patterns in how others have solved similar issues. Problem-solving with code is as much about mindset as it is about syntax.

Collapse
 
rosemediahouse profile image
Rose Media House

Back in the day, I used to do codeforces. good times.

Collapse
 
anitaolsen profile image
Anita Olsen

Oh wow, that sounded like an awesome one!

Collapse
 
rosemediahouse profile image
Rose Media House

Yes, although I was not very good at solving. My highest rating was 1150, which was considered mid.

Thread Thread
 
anitaolsen profile image
Anita Olsen

That is well done as well :)

Collapse
 
ayesha_03 profile image
Ayesha Tariq

I totally agree. Problem-solving is the real core of programming. My usual approach is to break the problem into smaller chunks, write down what I think should happen step by step, and only then start coding. I also try to rewrite the problem in plain language before jumping into syntax.

Funny enough, I sometimes take short breaks when stuck, like browsing random things to reset my brain. The other day I ended up scrolling through the kfc menu while debugging a tricky function πŸ˜…. It worked, though the solution clicked right after!

Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more