Data Structure and Algorithm ( Problem Solving Approach )

After learning JavaScript, I decided to try my hands on hackerrank's basic problem-solving assessment and when I tell you, I froze! I couldn't break down the question as it was really abstract. I didn't even understand the question enough to attempt breaking it down until the time was up. I knew then I had some work to do. First, I went back to learning JS from scratch on freeCodeCamp, attempting questions daily and just generally building my knowledge on it. I afterward, joined leetcode and again, it was still difficult which is why I'm learning DSA as a course on its own to further help my problem-solving skills.

I have written about the general problem-solving guide earlier, but now we are going to explore the problem-solving approach and techniques specific to DSA. This is a really important skill to have as a programmer and these days- learning the code and syntax is simple, solving problems is the hardest part but the more you practice, the better you get at it.

Let's get into it.

What is an Algorithm

This is a process or set of steps to accomplish a certain task.

How Do We Improve In Problem-Solving?

  • Create a plan for solving problems
  • Find patterns and master them.

Problem-Solving Steps

  • Understand the Problem
  • Explore Concrete Examples
  • Break It Down
  • Solve/Simplify
  • Look Back and Refactor

Understand the Problem

When solving a problem, don't just jump into it. Take a step back and understand the problem in front of you. Seek some clarification by asking yourself these questions;

  • Can I restate the problem in my own words?
  • What are the inputs that go into the problem?
  • What are the outputs that should come from the solution to the problem? what should they look like?
  • Can the outputs be determined from the inputs? In other words, do I have enough information to solve the problem?
  • How should I label the important pieces of data that are a part of the problem?

Explore Concrete Examples

Coming up with concrete examples can help you understand the problem better. Examples also provide sanity checks that your eventual solution works how it should. An example will be a Unit test. We can do this by using these steps ;

  • Start with simple examples
  • Progress with more complex examples
  • Explore examples with empty inputs
  • Explore examples with invalid inputs.

Break it Down

Explicitly write out the steps you need to take. This forces you to think about the code you will write before you write it, and it helps you catch any lingering conceptual issues or misunderstandings before you dive in and have to worry about details (e.g. language syntax) as well.

Solve/Simplify

If you have enough clarity, solve the problem but If you can't solve the problem at the time or are stuck on something, solve a simpler problem.

Simplification

  • Find a core difficulty in what you are trying to do
  • Temporarily ignore that difficulty
  • Write a simplified solution
  • Then incorporate that difficulty back in.

Look Back and Refactor

Refactoring is really important. Looking back and analyzing your code will help optimize your code better. You can do this by asking yourself these questions;

  • Can you check the result?
  • Can you derive the result differently?
  • Can you understand it at a glance?
  • Can you use the result or method for some other problem?
  • Can you improve the performance of your solution?
  • Can you think of other ways to refactor?
  • How have other people solved this problem?
  • If you work for a company, does it follow the company's guidelines?