DEV Community

Cover image for Intro to Data Structures + Algo [Part 2]
Miguel Ben
Miguel Ben

Posted on

Intro to Data Structures + Algo [Part 2]

Intro

Hey guys, it's another week for our coding challenges (Data Structures and Algorithms). Today we are going to be talking about Arrays which is one of the fundamentals Data Structures. However, in layman terms what is a data structure? It simply is things that we could built from scratch, for example an array. This is one of the most common topics in interviews, and you will usually see questions such as filter X elements from the array, reverse that other array or even start sorting this array.

The best way for you to solve an array based question is to simply be more knowledgeable about this topic. Moreover, knowing more about fundamentals such as operators, loops and recursion will help. Another key benefit is that it offers fast O(1) search if you know the index, but adding and removing an element from an array is slow because you cannot change the size of the array once it's created.

hohoo

Hopefully, I do have your attention

Examples

Before I forget, the following array methods have specific time complexities that you should keep in mind. The push() is a constant O(1), lookup() is O(1), insert() is O(n) & delete() O(n).

const ratings = ["M", "A", "X", "F"]
//                0    1    2    3  

ratings.push("N") // O(1)

ratings.unshift("T") // O(n)

ratings.splice(2, 0, "potatoes"); // O(n/2) simplified to O(n)

Great, now let's go over what are static and dynamic arrays. First, static arrays are the ones fixed in size, this means you are to specify ahead of time the number of elements your array will hold (Seen this more on C++ and Java). A Dynamic array allows us to copy and rebuild the array at a new memory location. What you need to remember is that Dynamic arrays expands as you add more elements, so you don't need to determine the size ahead of time.

Note: You should treat string questions as an array question.

wait..what!

Strings are simply an array of characters.

A few array coding questions to practice with:

  • [EASY] Create a function that reverses a string. Solution
const reverse = str =>{ // leaving this here to get you started

}

reverse("Pikachu loves his trainer")
// reniart sih sevol uhcakiP

  • [EASY-MED] Given two arrays that are sorted can you merge them into one big array (still sorted). Solution
const mergeSortedArrs = (arr1, arr2) => {

}

mergeSortedArrs([1,12,28,53], [4, 7, 22, 37])
// output => [1,4,7,12,22,28,37,53]

If you guys want to read a more amazing blog about this topic, please check on Sean posts.


Conclusion

We are just beginning to cover early on on Data Structures and so far we seen Big O and Arrays. We mentioned how dynamic arrays have to double up or expand our memory. Also how strings are considered array questions. It's recommended to go and cheat the Big O cheat sheet and read more the section regards array (interpret / understand). This will help you more by the time we get into Algorithms.

running

See you next time ~~~ Bye.

Top comments (0)