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)
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
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.
A good teacher on youtube once said:
This approach really worked for me!
Right
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.
I have learnt the hard way that diving headfirst into code is a bad idea.
My process is simple.
This reduces time spent figuring out how stuff work when writing the actual code.
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:
This method work even complex problem, when state are some stream.
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 π )
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.
Back in the day, I used to do codeforces. good times.
Oh wow, that sounded like an awesome one!
Yes, although I was not very good at solving. My highest rating was 1150, which was considered mid.
That is well done as well :)
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