Intro.
Why do we need data structures? Well without data structures, we cannot solve algorithms. In that case, if we were only coding for joy, we wouldn't necessarily need algorithms either. But for those who need to see the green, including myself, algorithms are a powerful necessity to prosper in coding interviews. Also, you'll run into data structures more often than not during your programming journey. Data structures have a particular way of organizing data on your computer to be used effectively. If your goal is to build better computer programs, understanding the concept of data structures will definitely help coding abilities become more efficient. At first glance, learning data structures from books, tutorials, etc can be really intimidating, solely based on the heavy presence of math and numbers. If you understand the the reasoning, they become less complicated.
Need To Know Data Structures!
So lets think of a way to break this down and get a better understanding! Imagine you were going to bake a cake, and the only ingredient you had was flour. Well you would't be able to bake that cake, unless you had all the rest of your ingredients, like sugar, vanilla extract, eggs, etc. Now if we combine all the necessary ingredients, we're in business and we can get baking! The same analogy works with data structures, it renders useless with a single data item, but when grouped together then we can consider those data items as useful compound data. As a result, that data gets stored in a particular data structure, and choosing the right one is important. There isn't a data structure that would be considered best to use, each one has its pros and cons. The way we can get a better understanding of choosing a data structure to manipulate our data is to see how it adds, retrieves, sorts, or searches those items.
.add()
.get()
.sort()
.search()
This is known as The Big O Notation. It describes how fast a function is growing. So, if we had a group of data items, and we added a significant amount more to our existing function, then it calculates how much longer each operation would take.
Data Structures.
- Linked Lists
- It's good at adding nodes
- It's also does well at deleting nodes because we can simply change where our pointer points to.
- It doesn't do so well at retrieving or searching nodes because it is only aware of the node that is next to it.
A linked list is a linear data structure that consists of nodes. A node consists of a value, which can simply be a number, and a reference link that points to the next node in that list. Hence, it being a linear structure that keeps going on and on. The start of a linked list is known as the head, where as the last node, is known as the tail or null.
Pros.
Cons.
- Arrays
- It's good at retrieving and searching because items are stored in specific memory locations. Which makes them easy to retrieve.
- Adding items can sometimes cause issues; as your array grows in size, it can crash into other items stored in memory.
Arrays have a familiarity across almost all programming languages, so you should probably be familiar with them. An array is a collection of data items stored in adjacent memory locations. Arrays keep memory of all the locations of the data items.
Pros.
Cons.
- Hash Table
- It's good at adding, retrieving, and removing because items don't crash based on memory location, like arrays.
- Sometimes two keys can hash to the same value, which is known as collision. This can be fixed with collision resistant hash functions, such as cryptography. Cryptography uses an algorithm to transform values, so they don't return mimicked values.
A hash table stores a collection of keys and values. It's an important type of data structure, because after you give a hash table a key, it's able to return its value. Even though it's similar to an array, what makes hash tables special is once you provide a hashing function, it automatically retrieves the data and it does not have to be stored next to one another like an array.
Pros.
Cons.
- Stack & Queue
- Efficient in adding and removing.
- Depending on use, usage could be limited, depending on your application.
Stack and queues are very similar to each other, and they are built from the same structure as arrays. With stack, the last item you put in is the first item to go out. Two methods used are .push() and .pop() to carry out this behavior. On the other-hand, queue operates as first item in, is the first item to go out. Adding an item to the end is known as .enqueue() and removing an item from the front is .dequeue()
Pros.
Cons.
These are some important data structures to keep in mind, next I'd like to cover graph and trees in its own right, since there's a ton of information to go over. Till next time coders... Below are some resources to study algorithms in preparation for your coding interviews!
Top comments (0)