You might be one of them who is happy with learning a new language or frameworks and building some applications with that but once you will enter in the real world industry, you will realize that your job is not just writing the code and make things work. Your real job is to write the right amount of good code which means it should be efficient and robust and here comes the role of data structures and algorithms. Data Structures and Algorithms not only help in getting the logic for your program but also helps in writing the efficient code for your software. Whether we talk about the time complexity or memory management, code refactoring or code reusability, you will understand it’s value in each part of your application.
In real-world projects, your brain should be able to write a quick efficient solution for complicated stuff and you can only do that when you practice a lot of coding questions. Understand that language and frameworks are just tools, it won’t teach you problem-solving skills. You develop problem-solving skills when you practice a lot of coding questions. Firstly understand the importance of DSA with these two articles…
- Why Data Structures and Algorithms Are Important to Learn?
- I Can’t Use Logic In Programming. What Should I Do?
Every developer has their own tricks and they follow their own pattern to solve coding problems but when it comes to new developers they are always uncertain about where to start. A lot of them understand the problems, the logic, and basics of syntax, they also understand someone else codes and they can follow along with them but when it comes to solving the questions on their own, they get stuck. They don’t understand how to turn their thoughts into code even though they understand the syntax or logic. We are going to share some simple steps that will help you to approach a coding question.
In real-world projects, your brain should be able to write a quick efficient solution for complicated stuff and you can only do that when you practice a lot of coding questions. Understand that language and frameworks are just tools, it won’t teach you problem-solving skills. You develop problem-solving skills when you practice a lot of coding questions. Firstly understand the importance of DSA with these two articles…
- Why Data Structures and Algorithms Are Important to Learn?
- I Can’t Use Logic In Programming. What Should I Do?
Every developer has their own tricks and they follow their own pattern to solve coding problems but when it comes to new developers they are always uncertain about where to start. A lot of them understand the problems, the logic, and basics of syntax, they also understand someone else codes and they can follow along with them but when it comes to solving the questions on their own, they get stuck. They don’t understand how to turn their thoughts into code even though they understand the syntax or logic. We are going to share some simple steps that will help you to approach a coding question.
Understand and Analyse the Problem
It doesn’t matter if you have seen the question in the past or not, read the question several times and understand it completely. Now, think about the question and analyze it carefully. Sometimes we read a few lines and assume the rest of the things on our own but a slight change in your question can change a lot of things in your code so be careful about that. Now take a paper and write down everything. What is given (input) and what you need to find out (output)? and while going through the problem you need to ask a few questions to yourself…
- Did you understand the problem exactly what is?
- Would you be able to explain this question to someone else?
- What and how many inputs are required to solve error?
- What would be the output for those inputs?
- Do you need to separate out some modules or parts from the problem?
- Did you Collect enough information to solve that question? If not then read the question again or clear it to the interviewer.
It doesn’t matter if you have seen the question in the past or not, read the question several times and understand it completely. Now, think about the question and analyze it carefully. Sometimes we read a few lines and assume the rest of the things on our own but a slight change in your question can change a lot of things in your code so be careful about that. Now take a paper and write down everything. What is given (input) and what you need to find out (output)? and while going through the problem you need to ask a few questions to yourself…
- Did you understand the problem exactly what is?
- Would you be able to explain this question to someone else?
- What and how many inputs are required to solve error?
- What would be the output for those inputs?
- Do you need to separate out some modules or parts from the problem?
- Did you Collect enough information to solve that question? If not then read the question again or clear it to the interviewer.
Go Through The Sample Data And Examples Thoroughly
When you try to understand the problem take some sample inputs and try to analyze or compiling the output. Taking some sample inputs will help you to understand the problem in a better way. You will also get the idea that how many cases your code can handle and what all can be the possible output or output range.
here are some important point to carry in your mind :
- Consider some simple inputs or data and analyze the output.
- Consider some complex and bigger input and identify what will be the output and how many cases you need to take for the problem.
- Consider the edge cases as well. Analyze what would be the output if there is no input or if you give some invalid input.
When you try to understand the problem take some sample inputs and try to analyze or compiling the output. Taking some sample inputs will help you to understand the problem in a better way. You will also get the idea that how many cases your code can handle and what all can be the possible output or output range.
here are some important point to carry in your mind :
- Consider some simple inputs or data and analyze the output.
- Consider some complex and bigger input and identify what will be the output and how many cases you need to take for the problem.
- Consider the edge cases as well. Analyze what would be the output if there is no input or if you give some invalid input.
Simplify The Problem
When you see a coding question that is complex or big, instead of being afraid and getting confused that how to solve that question, break down the problem into smaller chunks and then try to solve each part of the problem. Below are some steps you should follow in order to solve the complex coding questions…
- Make a flow chart or a UML for the problem in hand.
- Divide the problem into sub-problems or smaller chunks.
- Solve the subproblems. Make independent functions for each subproblems.
- Connect the solutions of each subproblems by calling them in the required order, or as necessary.
- Wherever it’s required use classes and objects while handling questions (for real-world problem like management systems, etc.)
Always Write Pseudocode
Before you jump into the solution it’s always good to write pseudocode for your problem. Basically, pseudocode defines the structure of your code and it will help you to write every line of code that you need in order to solve the problem. Reading pseudocode gives a clear idea that what your code supposed to do. A lot of people or experienced programmers skip this step but when you write pseudocode the process of writing the final code becomes easier for you. In the end, you will have to only translate each line of pseudocode into actual code. So write down every step and logic in your pseudocode.
Before you jump into the solution it’s always good to write pseudocode for your problem. Basically, pseudocode defines the structure of your code and it will help you to write every line of code that you need in order to solve the problem. Reading pseudocode gives a clear idea that what your code supposed to do. A lot of people or experienced programmers skip this step but when you write pseudocode the process of writing the final code becomes easier for you. In the end, you will have to only translate each line of pseudocode into actual code. So write down every step and logic in your pseudocode.
Replace Pseudocode With Real Code
Once you have written the pseudocode its time to translate this into actual code. Replace each line of your pseudocode into real code in the language you are working on. If you have divided your problem into subproblems than write down the code for each subproblem. While writing the code keep in mind three things…
- The point where you started
- Where are you right now?
- What is your destination (end result)?
Don’t forget to test your code with sample sets of data to check if the actual output is equal to the expected output. Once you are done with coding you can get rid of pseudocode.
When you are writing the code in your interviews keep telling the interviewer about how you are approaching the problem.
- Tell the interviewer how you are trying to start
- Tell the interviewer about your approach to solve the problem
- Discuss with the interviewer about the most difficult part you are facing in your problem.
- Tell the interviewer about the approach to solve each sub problem in order to get the final output.
- Discuss the sample data or test cases with the interviewer.
- Discuss about the better solution with the interviewer.
Once you have written the pseudocode its time to translate this into actual code. Replace each line of your pseudocode into real code in the language you are working on. If you have divided your problem into subproblems than write down the code for each subproblem. While writing the code keep in mind three things…
- The point where you started
- Where are you right now?
- What is your destination (end result)?
Don’t forget to test your code with sample sets of data to check if the actual output is equal to the expected output. Once you are done with coding you can get rid of pseudocode.
When you are writing the code in your interviews keep telling the interviewer about how you are approaching the problem.
- Tell the interviewer how you are trying to start
- Tell the interviewer about your approach to solve the problem
- Discuss with the interviewer about the most difficult part you are facing in your problem.
- Tell the interviewer about the approach to solve each sub problem in order to get the final output.
- Discuss the sample data or test cases with the interviewer.
- Discuss about the better solution with the interviewer.
Its time to Optimize your Codes
Always try to improve your code. Look back, analyze it once again and try to find a better or alternate solution. We have mentioned earlier that you should always try to write the right amount of good code so always look for the alternate solution which is more efficient than the previous one. Writing the correct solution to your problem is not the final thing you should do. Explore the problem completely with all possible solutions and then write down the most efficient or optimized solution for your code. So once you are done with writing the solution for your code below are some questions you should ask yourself.
- Does this code run for every possible input including the edge cases.
- Is there alternate solution for the same problem?
- Is the code efficient? Can it be more efficient or can the performance be improved?
- How else can you make the code more readable?
- Are there any more extra steps or function you can take out?
- Is there any repetition in your code? Take it out.
Always try to improve your code. Look back, analyze it once again and try to find a better or alternate solution. We have mentioned earlier that you should always try to write the right amount of good code so always look for the alternate solution which is more efficient than the previous one. Writing the correct solution to your problem is not the final thing you should do. Explore the problem completely with all possible solutions and then write down the most efficient or optimized solution for your code. So once you are done with writing the solution for your code below are some questions you should ask yourself.
- Does this code run for every possible input including the edge cases.
- Is there alternate solution for the same problem?
- Is the code efficient? Can it be more efficient or can the performance be improved?
- How else can you make the code more readable?
- Are there any more extra steps or function you can take out?
- Is there any repetition in your code? Take it out.
Finally time to execute your Codes
Note: If your problem is too complex, divide it into small problems; it’s a technique called “Divide and conquer”.
Now Execute your codes and try to make your process better, if there is an easier way to do it, or if there are some steps you can cut to simplify it (like loops). This step is very important—remember that it’s much easier to reconstruct your process in your head than it is in your code.
At this point, you will be tempted to write some code. Don’t do it yet! We have one more step to cover, and I promise you it will make your final code easier to write.
No comments: