Nothing is such “A Complicated Algorithm “

Image for post
Image for post

I am not a Technical Blogger, and everyone who knows me or went to Flatiron with me knows that. But I love Algorithms, Puzzles and a good challenge.

There is nothing excites me more than a complicated array of arrays, or object of objects, or a mix!!! And I’d love to share my secret of how to solve any complicated problem and how I overcame my weaknesses when I started learning Algorithms.

1- There is nothing called Hard or Easy Algorithm!

The word Algorithm isn’t just used to for engineers to fix problems, it could be anything from a recipe of your favorite dish, to the steps of Salsa dancing in the club. It’s simply a set of instructions to perform, fix or make something nice!

2- For code challenges and Interviews

Calm down, read and research anything in the instructions you are not familiar with. Remember you’re giving the instructions in English.

Also remember that the interviewer is looking for a team member, so consider him your teammate to help you with any clue or hints you may find helpful.

“Put me in a Team and I’ll fix any problem ” — Mokhtar Ali :)

I will explain in This complicated challenge I got on

The distance formula can be used to find the distance between two points. What if we were trying to walk from point A to point B, but there were buildings in the way? We would need some other formula..but which?

Manhattan distance is the distance between two points in a grid (like the grid-like street geography of the New York borough of Manhattan) calculated by only taking a vertical and/or horizontal path.

Complete the function that accepts two points and returns the Manhattan Distance between the two points.

The points are arrays containing the x and y coordinate in the grid. You can think of x as the row in the grid, and y as the column.


manhattanDistance( [1, 1], [1, 1] ) // => returns 0
manhattanDistance( [5, 4], [3, 2] ) // => returns 4
manhattanDistance( [1, 1], [0, 3] ) // => returns 3

In this code challenge, I have found lots of things that I am not familiar with, such as Grids and Manhattan Geography.

I quickly googled “Grid system for maps” and I got this helpful article:

The map explains how many lines go from PointA to PointB using 2 arrays.

Image for post
Image for post
PointA = [7,4]
  • If you look closer the blue star represents pointA which is [7, 4], where X is 7, and Y is 4.
  • PointB is an array of [5,2], both points on the map will look like this, as the top blue star is the coordinate of PointA, and the bottom Black star is the coordinate of PointB.
Image for post
Image for post
PointB = [5,2]


Don’t hesitate to try any idea, even if you think it’s stupid or won’t fix it!!

  • At this point I understood that from PointA to PointB I have to walk 4 blocks “lines”. But how can I put this in code?
  • This the simple part, you have two arrays, and each array = [X,Y], if you deduct X of PointA from X of PointB and vice versa for Y, you’ll get 2 numbers.
  • Add them together, and you’ll get the number of blocks between the 2 points!
let x = PointA[X] - PointB[X]
let y = PointA[Y] - PointB[Y]
let distance = x + y


Use Math.abs() when you add the distance because you may get a negative number which is a fair distance between 2 streets or points.

Math.abs(-2) gives the absolute value of -2 which is 2.

function manhattanDistance(pointA, pointB){
let x = PointA[0] - PointB[0];
let y = PointA[1] - PointB[1];
let distance = Math.abs(x) + Math.abs(y);
return distance;

PointA[0] is the first number of the array which is X, and PointA[1] is the second number of the array which is Y.

Image for post
Image for post

I hope this was helpful!

Mokhtar Ali, Full Stack Software Engineer | Designer | Marketing | Accounting | Blogger | World Traveler | NYC

Check out my Traveling Blog:

Web Developer | World Traveler | Positive Attitude

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store