Why should you learn data structures and algorithms to become a better programmer?

Bibin Sebastian
4 min readAug 7, 2020

This is a question I have debated over multiple times in my mind and I am sure this is a question several of you might also have. Let me try to reason out why I think you should learn Data Structures (DS) and Algorithm.

Data Structures and Algorithms? What are they?

Data Structures as it name says is a structure for storing data. Basic data types are array, list, stack, queue, map, heap … etc.
Algorithms are different ways of solving a problem. For example how to you sort list of items, how you find an element … etc.

Ok, Why are they important?

“You can’t build great buildings on weak foundation”. Consider DS and Algorithm as the foundation concepts to build any great software.

All the basic DS and algorithms are already inbuilt for you in most of the programming language libraries. They exist for a reason. You can solve a problem in an efficient/optimised way when you know the apt DS or Algorithm to be used. Especially when you are building a software which has to handle large volume of data, choosing the right DS and Algorithm is of paramount importance for it to scale.

Below are some examples to illustrate the point.

Let’s say that you need to sort a list of 1 million records. How do you do it?

There are different ways to sort the list and hence several sort algorithms as well. For instance if you use “bubble sort” approach, its takes O(n*n) time to sort while a “merger sort” can do the same sort in O(n log n) time.
So “merge sort” will perform far better vs a “bubble sort” approach when the value of n is high.

How do you search for an element from a list of 1 million records?

If you iterate over the list and search for an item, you can do it in O(n) time, but if you use binary search approach you can do in O(log n).
Higher the value of n, binary search will perform far better.

Do you know that when you create a database index to make your query faster, it uses binary search internally?

Let’s say you have collection of 1 million items and you need to filter out few thousand items from the collection. Assume that the items in the collections are unique. How do you do it?

Straight forward approach would be to use two for loops. Outer loop will iterate over the million items and the inner loop iterate over the 1000 items and you have a condition to filter out the matching items. Time complexity of this approach would be O(m * n). Can we simplify this?
Since the items are unique, you can use a hash set here to store the 1 million items. Then you can filter our the items in O(n) times. This is because you can look up an item in a hash set in O(1) time.

What type of list would you use when you want to make lot of insert/delete operations on the list?

If insert/delete operations are not done on the end of the list, linked list will perform better than an array list. Why? insert/delete on random index on an array list takes O(n) time on an average while linked list will only take O(1) time.

You need a data store with key value pairs but with ordering? How do you do it?

You can use a tree map here. But insert/delete/search operations takes a O(log n) time as it is backed up by a AVL or red black tree.

Do you know how do google maps finds the optimal path between origin and destination?

This is a graph problem isn’t it? How do you traverse between two nodes in a graph? How do you find the short path? There are well known graph algorithms to solve similar problems.

Is this hard to learn all these?

On work, you get all the basic DS and algorithms free as part of any modern programming language libraries. Being aware of features (operations, time and space complexities) of various DS and algorithms is all you need to build your solution. Also in many cases you have to compose them together to solve a particular problem. When given a problem, first think about the best possible DS/algorithm to solve it and over the time you will see you master the skill.

Another reason you may want to learn them is because every top tech company in the world tests you on these foundation concepts while hiring. Who doesn’t want to work for these top companies ? :) so you better learn these.

Conclusion

Learning data structures and algorithms helps you to build efficient solutions to the problem and hence helps you become a good programmer.

--

--