How to solve a programming question?
Here are 6 steps to follow while solving a programming question.
Solving a programming question has always been considered an impossible task to do. You don't need to be a wizard to solve a given problem. You just need know certain steps but you might not be solve the program on the first go. You will need a lot of practice to solve the problem consistently. Remember guys, "There is no shortcut". Don't consider this as a "hack" blog and have the thought that "You are prepared for an interview" and take a nap by just reading through the points, then you have to immediately say "BYE! BYE!" to those dream companies.
There is always a "WHY" question behind everything that we are doing. For example, "Why do we need to brush our teeth?". The answer is pretty simple and straightforward, "To keep my teeth clean and be able to speak with anyone without any awkwardness". But if the question "Why do we need to solve these problems?" arises, there will be bunch of people telling that, "OH! THE QUESTION IS VERY EASY TO ANSWER, WE SOLVE THESE TO TRAIN FOR THE INTERVIEWS". Like seriously. Is this the real reason behind your effort on those recursion problems that you are trying to solve? No. The core idea behind these coding questions is designed such a way that it helps to improve your problem solving skill. This problem solving skill is going to make a better programmer and a developer. If you have got a question "How?", the simple answer to that question is we developers' main job is to solve everyday problems. So, everything is connected with each other.
So, where do we need to practice such coding problems? There are some platforms that are really awesome to jump right away even for beginners to solve problems.
Codewars
HackerRank
HackerEarth
CodeForce
Leetcode
I will be taking examples from leetcode to explain the steps. You can surely use any platform to start coding. The main goal here is to solve problems.
PRE REQUISITES
Learn about Data Structures and Algorithms
DSA is a must to learn any coding question. By learning you can complete the 60% of the problem. Only if you learn the DSA concepts clearly, you will be able to approach to an optimized and well structured solutions.
DSA can be learnt in many programming languages. The popular languages include Java, C++, Python and Javascript.
You can search the questions according to the DSA concept that you have learnt by selecting the topic from the tags drop-down option.
Start out with easy problems.
Always start out with the easy problems. If you start with the easy problems first, it builds the confidence that is required to continue the learning process. Not only that, it builds the foundation that is needed. These foundational knowledge can then be applied to medium and hard problems.
You can find easy problems in leetcode by selecting Easy from the difficulty drop-down list.
Make notes.
You can use excel to make list of how you solved the problem. You can make use of GITHUB where you save your code sample in the same place. These platforms will help you to revise the dsa problems.
1. Read the problem till you understand the problem.
If you don't understand the problem, you can't solve the problem. Read till you get the concept of the problem. Understand what the problem wants. After this you will get an outline what is the problem is all about.
If you still don't get any idea of what the problem is about and how to come up with the solution, don't worry about that. Go to the next step.
2. Extract the input and know the output for the input.
Get the input from the problem. Know what data type the input is used, the return type and the output type.
In the above problem, there are n distinct number and the input parameter is in array data type. The return type is int here.
3. Break the problem into sub problem.
First thought that should arrive in your mind is "what algorithm should be used in this problem". This should be the problem that you should be addressing. Second problem is "how can you solve this problem by using algorithm". So on and on, you can divide your problem into various sub problems.
Here the algorithm that can be used is cycle sort.
4. Come up with Brute force approach.
You can't come up with correct and best solution at the first attempt. You can always get to the brute force solution. So what is Brute force solution? In Brute force solution, you don't have to worry about either the time and space complexity. The only thing that you should keep in mind is "how can I come to the solution to the problem".
The brute solution here can be arrived if you didn't use the cycle sort algorithm. The brute approach may be:
Get a iterative loop with variable "i" that starts from 0 and ends till n.
In every loop, there can be an another loop that can be used to iterate over the array to check whether the number "i" that is in the outside iterative loop is present.
As you can see this approach takes a more time complexity than it supposed to taken.
5. Optimize your code.
Now that you have approached to a brute force solution, you still have room for improvement. Think of more optimized. At this stage you can get help of pen and paper. Sketch out the working of the input and output that can be arrived using the given input. Try to get the pattern of the working with different inputs.
The time complexity that can be arrived at the end of the code is O(N).
6. Look for improvements.
If you can't arrive to the solution to the problem, you can take help of youtube and other editorials. You take the same approach and algorithm that is used.
In leetcode, you have this option know as Discuss where there are many solutions by other programmers. You can also specify the target language using the Tag option in the right of the window.
Final Notes
Now that you know the steps that need to be followed while solving a problem, remember to practice every single day. You need not be an expert to solve. When you consistently build on the problem solving skills, you will able to solve and come up with the solution a lot easier than before. Make it as an habit and do this for just 30 days to see the difference in hand. You will be amazed to know how your brain has evolved in the result of consistency.
Final note is that you practice, practice and practice.