How to approach an algorithm

Udemy Course: JS Algorithms & Data Structures Masterclass

Anney Park
3 min readJan 12, 2021
the meme everyone has felt at least once in their life

So what exactly is an algorithm?

It’s a series of step by step instructions that are followed to solve a problem.

I came to find out that there are different types of algorithm problems since different types of problems require different types of algorithmic-techniques to be solved in the most optimized manner. I will be briefly discussing some key terms and concepts that are necessary to learn for a successful technical interview.

Before we dive into the types of algorithms I first want to go over how to approach an algorithmic problem no matter what type it is.

1. Understand the problem:

  • Can I reword the problem so that I can better understand it?
  • What are the inputs that go into the problem?
  • What are the outputs that should come from the solution?
  • Do I have enough info to solve the problem?
  • How should I label the important pieces of data that are a part of the problem?

2. Explore concrete examples:

  • Start with simple examples and progress to more complex ones
  • Explore examples with empty inputs and invalid inputs

3. Break it down:

  • Explicitly write out the steps you need to take. It helps you think about the code and helps catch any conceptual issues or misunderstandings you might come across before you dive in and worry about language syntax or any other details

4. Solve or simplify:

  • Find the core challenge in what you’re trying to do
  • Temporarily put aside that difficulty & write a simplified solution

5. Look back & refactor:

  • Can you check the result? Improve it? Refactor it? Do it differently?

Useful terms to know

Intro to Big O Notation: Big O describes the performance & efficiency of a code.

  1. O(1) — Constant
  2. O(n) — Linear
  3. O(n log n) — Linearithmic
  4. O(log n) — Logarithmic
  5. O(n²) — Quadratic
snapshot from Udemy’s Course: JS Algorithms & Data Structures Masterclass
Source of Graph: http://bigocheatsheet.com

Time complexity: how can we analyze the runtime of an algorithm as the size of the inputs increases

Space complexity: how much additional memory do we need to allocate in order to run the code in our algorithm

Higher Order Functions: Functions that operate on other functions, either by taking them as arguments or by returning them. Ex: Array.prototype.map and Array.prototype.filter

Higher Order Function example using .map:

const array1 = [2, 4, 6];
const array2 = array1.map(num => num * 2);
console.log(array2); // [ 4, 8, 12 ]

--

--