How to learn data structures and algorithms

How to learn data structures and algorithms 1

Last Updated on

Mindset

As my previous article elaborated, data structure and algorithms are the ultimate basis for a programmer.  I strongly believe that a good knowledge of these two topics is also key to become a better programmer because a person who has a good understanding of algorithms and data structures can make thoughtful choices and write programs that can handle changes better and perform well.

I come, I conquer

Some beginners will be discouraged and be frustrated at the beginner,  feel that the data structure and algorithm are very abstract and obscure.

How to learn data structures and algorithms 2

Don’t worry, that is very normal. I also got the same feeling many years ago. The real reason is that you did not find your own learning method, and haven’t built confidence for it.  This is my roadmap and tips for you, Keep patience and keep moving.

Don’t be afraid of the mathematical part.

Data structure and algorithms do involve some mathematical reasoning and proof,  especially when analyzing the time and space complexity of an algorithm. The big-O complexity analysis is important, but you don’t need to worry about it at all.  You don’t need a high IQ or abstract mathematical knowledge. You can master it as long as you have a high school mathematics level.

You will have more confidence after months of practice and learning.  

What is the data structure and algorithm?

Although we say that there is no need to dig deep into the definition,  this does not mean that we do not need to understand the concept. Generally speaking, the data structure is a storage structure for a set of data, an algorithm is a method and pattern for solving problems.

I will help you understand the two concepts of data structure and algorithm from examples.

Take an example

How to learn data structures and algorithms 3

 

How do you find a book from a library?

  • Method #1: You can check each book one by one until you find the right book.
  • Method #2: You can first locate the bookshelf according to the number of the book category,  whether it is humanity, or science, computer, and then search in the bookshelf.    

In general terms, these two methods for finding a book are both algorithms, so we get the definition of an algorithm is: The methods for solving problems, which could be implemented in programming.

In details, when we talk about data structures and algorithms,  we are talking queues, stacks, heaps, binary search, dynamic programming, and so on.  These are patterns for abstraction and solving problems, which invented by computer pioneers.

We can effectively learn them and use them to solve many practical development problems.

The relationship between data structures and algorithm

Data structures and algorithms complement each other.  The data structure is for the algorithm, and the algorithm is applied to a specific data structure.  Therefore, we can’t isolate the data structure and algorithms.

How to learn data structures and algorithms 4

 

For example, because arrays have the characteristics of random access,  the commonly used binary search algorithm requires an array to store data.  But if we choose a data structure like a linked list,  the binary search algorithm will not work because the linked list does not support random access.

The Steps

Get good books and resources

There are some really good textbooks for data structure and algorithms, but at the beginning,  you maybe need the books which are coded in a programming language you have mastered.

Visualizations will help you understand how data structure and algorithms works 

How to learn data structures and algorithms 5

Learning the basics data structures

Many advanced data structures and algorithms, such as bipartite graphs, maximum streams, etc., are rarely used in our usual development.  So, you don’t have to look at them at the beginning.

There are 11 data structures:

array, linked list, stack, queue, hash table, map, binary tree, heap, 
jump table, graph, trie tree

You should learn the API for these data structures and also the complexity of their operations.

GeeksforGeeks summarized many good practices for data structures.

Learning basic algorithm design pattern

You need to learn the basic algorithms, usually, all the problems will be solved by some existing algorithms,  but some harder problems need some variants, you need to combine several algorithms to solve one problem.     

The most used algorithms come to these categories, you should focus on one specific category in a short time, try to learn them one by one:

recursion, sorting, binary search, search, string matching, 
hash algorithm, greedy algorithm,
divide and conquer algorithm, backtracking algorithm, 
dynamic programming

Learn by doing

There are many websites for practice data structures and algorithms, I recommend these two:            

  • Leetcode: which contains almost all the usual interview questions come from a big company, like Facebook and Amazon, it’s very helpful for interviewing
  • HackerRank: which has very clean categories for data structures and algorithms, and even mathematics. You can focus on one kind of data structure or algorithm, for example, spend one week focus on the greedy algorithm, then switch to another one. And you can also enter the coding contest for fun.

Another good practice is finishing some trivial projects which using one specific data structure or algorithm. For example, this project creates a Maze with the data structure of “union-set” and also tries to implement pathfinding algorithms. Finish this kind of projects will build your confidence, and also lets you realize it’s useful for creating real applications.

Get deeper

More books

<Introduction to Algorithms, 3rd Edition> is for your deeper studying, the algorithm reason part is awesome and also have more details on complexity analysis and mathematical tools.

And the final book is: <The Art of Computer Programming>, very few programmers have finished reading all this book. But this is the ultimate reference for you when you need to check out, this book almost covers all the corners of programming, especially about data structures algorithms. 

Learn how data structures and algorithms used in real projects

Redis contains many mostly used data structures, and also have many optimizations for performances. The Linux Kernel also used many data structures like link list, red-black tree, hash, etc. These great projects are the most useful resources for your learning.

Keep curiosity! After one or two years, the data structures will you tools for abstraction and design algorithms will be the natural pattern for your problem-solving.