Approaching the problem.

Dolly Desir
2 min readMar 15, 2021

As I am diving into the world of algorithms, I’m realizing I shouldn’t be so afraid of them. One of the biggest reasons I hate algorithms was not even being able to understand the question that is being asked. Problems are worded in a such a way that even understanding what I’m being asked to do doesn’t make sense. For example, “Find the k largest number in an unsorted array.”

First of all…who is k and why do they need the largest number?

After some googling I discovered this question is basically asking, given an array [2,4,6,1,24,33,40,12,16], you are being asked to find the a specific number amount of the largest elements in the array. Let’s the number was 3, k = 3 but because I’m convinced the problems are created with confusion in mind, the letter k is chosen, it’s just a variable. Here are some steps that I take.

  1. State the problem out loud in my own words. Doing this makes me take a moment to figure out what exactly am I being asked.
  2. Are there any inputs that go into the problem, if so, what are they? i.e. does my function need to take an argument? How many arguments?
  3. What are the outputs that should come from the solution? i.e. what should I be returning, do I even need a return statement?
  4. Do I have enough information?
  5. How should I label the important pieces of info in the problem? i.e. will I need a variable to keep track of information as it’s looping?
  6. Write out the steps I need to take…a.k.a sudo code. I can’t stress enough how helpful this has been for me. I still even try to keep track in my head, knowing that it only confuses me. Writing out each step makes a world of a difference.

So let’s say you do solve the problem, that isn’t it. There’s also questions about how can you make your code better.

  1. Can I do this differently?
  2. Can it be understood by someone else?
  3. Can I use the result of this code or the method for another problem?
  4. Can the performance be improved?
  5. How have other people solved this problem?

I’m learning that being able to refactor my code is just as important as solving it. Different methods can function very differently depending on the question being asked. I still struggle with algorithms but I’m developing a method on how to approach them rather being terrified and closing my computer. It’s a necessary skill that I’m allowing myself to be patient with to learn. Thanks for reading !

--

--