Codementor Events

I learned all data structures in a week. This is what it did to my brain.

Published Mar 21, 2018Last updated Sep 16, 2018

Over the last week, I studied seven commonly used data structures in great depth. The impetus for embarking on such a project was a resolution I made at the beginning of the year to train myself to be a better software engineer and write about things I learned in the process.

In the last 3 years since I first studied about them during my undergraduate studies, I felt no glimmer of temptation to study any one of them again; it wasn’t the complex concepts which kept me away, but their lack of usage in my day to day coding. Every data structure I’ve ever used was built into the language. And I’ve forgotten how they worked under the hood.

They were inescapable now. There are seven data structure in the series to be studied.

Let us go back to where it all began. Like every invention has a necessity, and having data structures also had one.

Say you’ve to find a specific book in an unorganized library. You’ve put in numerous hours in shuffling, organizing and searching for the book. The next day the librarian walks in, looks at the mess you’ve created and organizes the library in a different way.

Problem?

Just like you can organize books in a library in 100 different ways, you can structure data in 100 different ways. So, we need to find a way to organize our books(read: data) so that we can find our book(read: data) in a very efficient and fastest way.

Solution:

Luckily for us, some uber smart people have built great structures that have stood the test of time and help us solve our problem. All we need to know how they work and use them. We call them data structures. Accessing, inserting, deleting, finding, and sorting the data are some of the well-known operations that one can perform using data structures.

The first entry in the series Array’ leaves no need to have multiple data structures. And yet there will be so many more. I do not have the energy to describe why one data structure triumph over another one. But, I’ll be honest with you: it does matter knowing multiple data structures.

Still not convinced?

Let’s try to solve few operations with our beloved array. You want to find something in an array, just check every slot. Want to insert anything in middle? You can move every element to make room.

Easy-peasy, right?

The thing is “all of these are slow”. We want to find/sort/insert data efficiently and in the fastest possible way. An algorithm may want to perform these operations a million of times. If you can’t do them efficiently, many other algorithms are inefficient. As it turns out, you can do lots of things faster if you arrange the data differently.

You may think, “Ah, but what if they ask me trivia questions about which data structure is most important or rank them?”

At which point I must answer: At any rate, should that happen, just offer them this — the ranking of the data structures will be at least partially tied to problem context. And never ever forget to analyze time and space performance of the operations.

But if you want a ranking of learning different data structures, below is the list from most tolerable to “oh dear god”

You will need to keep the graph and trees somewhere near the end, for, I must confess: it is huge and deals with zillions of concepts and different algorithms.

Maps or arrays are easy. You’ll have a difficult time finding a real-world application that doesn’t use them. They are ubiquitous.

As you worked through the data structures series, you will realize one does not simply eat the chips from the Pringles tube, you pop them. The last chip to go in the tube is the first one to go in my stomach(LIFO). The pearl necklace you gifted your Valentine is nothing but a circular linked list with each pearl containing a bit of data. You just follow the string to the next pearl of data, and eventually, you end up at the beginning again.

Our Brain somehow makes the leap from being the most important organ to one of the world’s best example of a linked list. Consider the thinking process when you placed your car keys somewhere and couldn’t remember.Our brain follows association and tries to link one memory with another and so on and we finally recall the lost memory.

We are connected on Twitter. Thank you, Graphs. When a data structure called trees goes against nature’s tradition of having roots at the bottom, we accept it handily. Such is the magic of data structures. There is something ineffable about them — perhaps all our software are destined for greatness. We just haven’t picked the right data structure.

Here, in the midst of theoretical concepts is one of the most nuanced and beautiful real-time examples of the stacks and queues data structure I’ve seen in real life.

Browser back/forward button and browsing history

As we navigate from one web page to another, those pages are placed on a stack. The current page that we are viewing is on the top and the first page we looked at is at the base. If we click on the Back button, we begin to move in reverse order through the pages. A queue is used for Browsing history. New pages are added to history. Old pages removed such as in 30 days

Now pause for a moment and imagine how many times we, as both a user and developer, use stacks and queues. That is amazing, right?

But, my happiness was short-lived. As I progressed with the series, I realized we have a new data structure based on a doubly-linked list that handles browser back and forward functionality more efficiently in O(1) time.

That is the problem with the data structures. I am touched and impressed by a use case, and then everyone starts talking about why one should be preferred over other based on time complexities and I feel my brain cells atrophying.

In the end, I am left not knowing what to do. I can’t look at the things the same way ever again. Maps are graphs. Trees look upside down.These data structures look absolutely asinine yet I cannot stop talking/thinking about them. Please help.

Thanks for reading! If you liked this post, you can check out my other writings here or please consider entering your email here if you’d like to be added to my once-weekly email listor you can follow me on Twitter for article updates.

Discover and read more posts from Shilpa Jain
get started
post commentsBe the first to share your opinion
AMANAT ALI
5 years ago

is it a joke…

Arun
6 years ago

There the most fun to working the best image below the add printer according the window 10 setting click here http://addprinterwindows10.com/ and see the best service if you like as the web user connect by the USB cable.

manish
6 years ago

Please provide some examples of Graph. I don’t know when to use them. and plesase advice a blog or post or any learning material to understand Graph

Show more replies