In this post, we're going to discuss the problem-solving Approaches and how to come up with the optimum approaches for a problem. I recommend you to refer to this post before you are solving any problem. Whatever I'm discussing will help you in the longer run. Hope you will enjoy!
- Will discuss, about what an algorithm is?
- Will discuss, how to devise a plan to solve any problem?
- Will compare and contrast problem-solving patterns including frequency counters, two-pointer problems and divide and conquer
A process or set of steps to accomplish a certain task. (in simple terms)
The algorithm will help you to solve a smaller to bigger problem. It's a good practice to write an efficient algorithm all the time. So, we need to practice accordingly.
How do we improve our algorithmic skill?
To improve our skill we need to follow some rules before proceeding any problem.
- We need to plan before solving anything (like plan what data structures to use, what programming technique is suitable for this problem)
- Master common problem-solving patterns (will discuss in this post)
Checklist for problem-solving
- Understand the problem (observe the expectations)
- Explore concrete examples (play around with the sample inputs)
- Break it down into pieces.
- Solve and simplify.
- Look back and refactor the code.
Understand the problem
- Restructure the whole problem in your own words.
- Gather the information about the inputs of the problem.
- Gather the information about the expected outputs for the problem.
- Think about the output and ask yourself is it possible to produce the output with given inputs? and analyse, is the information given to you enough?
Example: Write a function which takes two numbers and returns their sum. First I will write in my own words as I understood the problem. After that, I'll analyze the given inputs and possible outputs for the same problem.
Explore examples(Test cases)
Explore examples means you don't need to come up with the working examples for this problem. Instead, come up with examples of inputs, possible outputs and try to understand the problem better.
- Start with simple examples (write it down the simple example inputs)
- Progress with more complex examples (write some bigger user stories).
- Explore examples with empty values
- Explore examples with invalid values.
Break It Down
Let's break the complex bigger problem into smaller steps to accomplish. write down the steps. This will also help to write better code. Because you already know the steps so you'll easily break it down into multiple functions for each subproblem and the code will also readable and maintainable.
Simplify the problem
- Find the more difficult part of the problem you are trying to solve.
- Forgot about that part temporarily and concentrate on other parts.
- After completing all the solvable parts go to the complicated part.
Refactoring the code
while refactoring the code try to convert it to more readable and performant. Comment the code well. Checklist for refactoring the code:
- Can you check the result?
- Can you derive the result differently?
- Can you understand it at a glance?
- Can you use the result or method for some other problem?
- Can you improve the performance of your solution?
- Can you think of other ways to refactor?
- How have other people solved this problem?
I won't say this is the best approach. The best approach doesn't exist. If you have any thoughts feel free to comment below.
Let's master the common problem-solving patterns in the next blog.