How To Approach A Coding Problem ?

 

Developers and students solve a lot of coding questions of data structures and algorithms but most of them don’t understand the importance of it. A lot of them also have this opinion that data structure and algorithms only help in interviews and after that, there is no use of all those complicated stuff in daily jobs.

How-to-Approach-a-Coding-Problem

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…

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)? While going through the problem you need to ask a few questions to yourself…

  1. Did you understand the problem fully?
  2. Would you be able to explain this question to someone else?
  3. What and how many inputs are required?
  4. What would be the output for those inputs
  5. Do you need to separate out some modules or parts from the problem?
  6. Do you have enough information to solve that question? If not then read the question again or clear it to the interviewer.

For Example: If you are given an array and you need to return the array that contains only even numbers than first of all analyze the problem carefully. While going through the problem a few questions you should ask yourself before jumping into the solution.

  1. How to identify even number? Divide that number by 2 and see if its remainder is 0.
  2. What should I pass into this function? An array
  3. What will that array contain? One or more numbers
  4. What are the data types of the elements in the array? Numbers
  5. What is the end goal? The goal is to return the array of even numbers. If there are no even numbers, return an empty array.

Go Through The Sample Data And Examples Thoroughly

When you try to understand the problem take some sample inputs and try to analyze the output. Taking some sample inputs will help you to understand the problem in a better way. You will also get the clarity that how many cases your code can handle and what all can be the possible output or output range. Read the points given below…

  • 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.

For Example: If you need to return the array of even numbers from a given array then below are some variety of caes or sample inputs for which you can anlyze the problem and it’s output.

[1]
[1, 2]
[1, 2, 3, 4, 5, 6]
[-300.35]
[-700.1, 1000, 5.1, -1000.25, 52, 900]

Break Down 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.)

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. Below is one of the examples of pseudocode that returns the array of even numbers…

function getEvenNumbers
evenNumbers = []
for i = 0 to i = length of evenNumbers
  if (element % 2 === 0) 
    add to that to the array evenNumbers
return evenNumbers

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 (we have discussed in step 2) to check if the actual output is equal to the expected output. Once you are done with coding you can get rid of pseudocode. Below is the code for the example we have considered in the previous steps…

filter_none

brightness_4

function getEvenNumbers(arrayofNumbers) {
  let evenNumbers = []
for (var i = 0; i < arrayofNumbers.length; i++) {
    if (arrayofNumbers[i] % 2 === 0) {
      evenNumbers.push(arrayofNumbers[i])
    }
  }
return evenNumbers
}

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.

Simplify and Optimize your Code

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.

Below is the alternate solution for the same problem of array which returns even numbers…

function getEvenNumbers(arrayofNumbers) {
  let evenNumbers = arrayofNumbers.filter(n => n % 2 === 0)
  return evenNumbers
}

Comments

Popular Posts