Categories
Data Structure & Algorithm

Am not too sure Problem Solving Approach in Data Structures and Algorithm is such a rocket science that we can’t handle. to every problem there should always be a way to solve them, that is what appears to be the Algorithms (the Steps involved in arriving at the answers) this type of non portamento way of solving problems is mostly seen in Mathematics, Physics, Chemistry where you have to do some proving to show case your answer isn’t just a lotto kiosk number. so let’s say in the nutshell Algorithms are pieces of procedures involved in solving an easy or difficult problems. One there is on thing you should never do especially in a mock up job interview is to jump right at the answer without having to show any steps involved. even if the question is no doubt one of those you have been solving during your preparation time. per experience one thing that happens most often when you take the jump approach is you’re likely to get stuck at a point where if care is not taken the confusion might grow to the level where you have to restart the process again but this time going with the right approach you neglected earlier

So what is the right approach ? do not worry the right approach is definitely not a by all standard one, you can equally device your own to approach any problem thrown at you in this field. But am going to discuss you with some of the smatter approaches you may take if you have no idea how to handle this part.

My take home advice : Even if you know the right answer to these DSA problems in front of an interviewer pretend to go through these steps to prove other traits in you that the recruiter might be looking for which is the problem solving skills or approaches. if they’re straight forward trust me you may be saying bye bye to that dream job you tough you perform well at. Okay that’s enough, if you give the whole day i will write nonsense like this for you and never get tired.

Given a problem, These are some five(5) steps to keep your thumbs on at all time before you start writing any code. not say and get stack and restart the process once again.

1. Understand The Problem 

understanding the problem should a half way through of the whole process to solve your problem. doing so will help you to gather enough information about the problem.

2. Explore Concrete Examples

most likely in a mockup interview DSA Questions you will be provided some sample cases that your solutions should run on. you need to take advantage of this to help you know what the expected results should look like. here state your inputs that go into the solution and the expected output. one more thing develop a way to think of the security of your solution at this level. ie what happens if— i insert wrong input, what are some constrains with the problem etc…

3. Break it down

If the problem is too overwhelming to you, don’t think the recruiter is wicked to put you through this hell, besides you can even use the recruiter to your advantage at this level asking him to clarify certain aspect of the problem which seems so challenging to you. playing around this step very well can help you in so many ways.  by Asking the right questions. Done with the above begin to break the problem down to pieces before you move on onto the next which is obviously. solving the problem now.

4. Solve/Simplify

by now you’re most likely to begin writing some codes, if along the way you don’t know or can’t remember a syntax you can ask the recruiter if he can help you that part word if you’re mocking unattended Horayyy  you can open your favorite search engine. don’t worry you don’t need to tell me, in my case is www.Google.com. no better place to be. Haaah

After you solve your problem, kindly test it to see if everything seem to work the way the problem wanted it to, at this time you might be thinking and singing Aleeluhaa what is i tell you you’re not done. your recruiter will most likely ask you if there is any better way of solving this so you need to be prepare for this as well. That brings us to number five of this discussion.

5. Look back and Refactor

Moment of truth. try to find a reason to convince yourself your answer is not at it best that it can be by looking for loop holes or flaws in your solution, you might find out some few tweaks that could have made your solution more performant. Please don’t push it if you can’t find any.  sit back and take a cup of coffee if you can’t find any.

Let’s look at a problem and put all that we saw above into practice.

Problem: Write a function which takes in a string and return counts of each character in the string

for this problem which seems very simple at the surface, let’s take a look at how to approach it at least the best way i can. i know you might be some kind of Guru reading this but please take it easy with me.

  • First thing i will probably think of after i am convince i understand the problem is: Write a function called countString(….) at this level i know my function is going to take in some input.
  • create an Object to keep our records since we need a key value pair here ie {a:1,b:3,c:1,….}
  • Loop over string for each character
  • if character is a letter/number and it already exists in our object then add +1 to count it’s state.
  • if character is a letter/number and it does not exists in our object then set it value to 1 to keep it’s state.
  • if character is some else apart from letter/number ie. Space, Symbol etc… do nothing
  • return the object as a result at the end of the loop.

having laid down these approach to take helps and guides you to keep track on yourself whilst solving this problem. not only this is helpful but also goes to prove to whoever is interviewing that you have at least if not good but a way to solve your problems. the above can also be termed as Pseudo Code if you take a more mathematical approach.

Solving the Problem

There are different ways to solve this simple problem. a solution might be different from the other one and for that matter their efficiency will definitely differ. let’s take a look at the First Solution.


function characterCounter(string){
    let obj = {}
    for(let i = 0; i<string.length; i++){ 
    let character = string[i].toLowerCase(); 
    if(/[a-z0-9]/.test(character)){ 
            if(obj[character]>0){
                obj[character]++
            }else{
                obj[character] = 1;
            }            
        }
    }  
    return obj;  
}
let input = "casrlos"
console.log(characterCounter(input));

running this solution gives you the desire output solution that lays out each character with the number of times it has appeared in the string provided as an input. ie. {c: 1, a: 1, s: 2, r: 1, l: 1, …}

Refactor and lookback : Remember the last Approach ? Lookback to see if there are better and simpler ways of solving this problem and Yes there is but you might think it’s too much for the second solution. let’s have a look



function characterCounter(string){
    let obj = {}
      for(let character of string){
        character = character.toLowerCase();
        if(isAlphaNumeric(character)){
            obj[character] = ++obj[character] || 1
        }
    }
    return obj;  
}

let input = "casrlos"
console.log(characterCounter(input));

function isAlphaNumeric(char){
    let code = char.charCodeAt(0);
         //numeric (0-9)
    if(! (code > 47 && code < 58 ) && 
       //uperCase Alphabets (A-Z) 
      ! (code > 64 && code < 91 ) && 
     //lowerCase Alphabets (a-z) 
     ! (code > 96 && code < 123 )){ 
           return false;
       }
    return true;
}

Looking at this second solution you might think it’s too long for you but it’s actually very simple we happened here is the just few things

  1. the for loop has been simplified to a much less threatening version. instead of a long one with math it’s been simplified take a look and compare.
  2. the regular expression if(/[a-z0-9]/.test(character)){ is actually a mush time consuming one than to create a separate function called isalphaNumeric() to check each characters using their charcodeAt() JavaScript inbuilt function.
  3. finally instead of dealing with a lot of if else, it has been simplified to a much lesser version.

If you care to check if really the second solution runs much better than the first you can run your performance.now() on them individually to see them play. but mind you that in each case there are some trade offs. it all depends on your scenario.

Okay my friend that is it for today , i have to go and until we meet again, Kindly Read and solve more questions, that’s my only guarantee you will be best at DSA.

Leave a Reply

Your email address will not be published. Required fields are marked *