Coder's Cat

How To Learn Data Structures And Algorithms (An Ultimate Guide for Beginners)

2019-12-24

Mindset

Data structures and algorithms are essential for any programmer.

I strongly believe that a thorough knowledge and skill of these two topics are the key to becoming a better programmer.

An engineer with a deep understanding of algorithms and data structures will be able to make informed design choices, and write programs that are more performant and easier to change.

I came, I saw, I conquered

As a beginner, you may feel discouraged and frustrated by these concepts. The terms data structures and algorithms both sound abstract and obscure.

file:img/it-is-hard-to-learn-data-structures-and-algorithms.gif

Don’t worry, though. That’s a normal reaction for a beginner.

I had the same reaction when I started learning about the subject. The reason you feel that way is that you haven’t found the right method to let you study the subject deliberately.

I’m going to tell you about my roadmap and tips. Be patient, and always keep moving forward.

Don’t be intimidated by mathematics

Data structures and algorithms do involve some mathematical reasoning and proofs, particularly when analyzing the time- and space-complexity of an algorithm.

Being able to perform a big-O complexity analysis is certainly important, but you don’t need to worry about it too much to start with. You don’t need a high IQ or abstract mathematical knowledge.

As long as you understand high school mathematics, you have the tools needed to understand data structures and algorithms.

That may seem unlikely to you right now. In this article, I’ll argue otherwise: after a few months of directed study and practice, you’ll be able to approach the subject with confidence.

What is data structure and algorithm essentially?

Generally speaking, a data structure is a way to organize data, while an algorithm is a method or pattern for solving problems.

To illustrate this, let’s say you want to find a specific book in a library.

Take an example

file:img/2019_09_12_learn-data-structures-and-algorihtms-library.png

How do you find a specific book from a library?

  • Method #1: You could check each book by shelves, one by one, first to last, until you find the book in question.
  • Method #2: You could first locate the bookshelf according to the category of the book in question. You determine whether the subject is the humanities, science, computer science and so on, and then you search only that specific bookshelf.

    Each of these methods are algorithms. This is the definition of an algorithm: a method for solving problems which can be implemented via programming.

    In this analogy, the books, the shelves, and the way they are arranged are all data structures.

    In concrete terms, when we speak about data structures, we are talking queues, stacks, heaps; when we say algorithms, we speak of binary search, dynamic programming, and so on.

    Together, data structures and algorithms represent tried and tested patterns for abstraction and problem-solving. They were invented by pioneers in the field. They did the hard work that we can leverage in order to solve many common development problems.

How data structures and algorithms relate

Data structures and algorithms complement each other. The data structure exists for the algorithm, and an algorithm generally suits a specific data structure.

file:img/2019_09_12_learn-data-structures-and-algorihtms-ds-and-algorithms.png

For instance, arrays are contiguous. The binary search algorithm applies to direct access of contiguous memory, so an array is used to store the data for a binary search algorithm.

If instead we want to use a different data structure such as a linked list, the binary search algorithm wouldn’t work, as the linked list data structure doesn’t support direct access.

The Steps

Pick a few good books and resources

There are several classic works on data structure and algorithms. They are useful resources, but can be difficult for a beginner. At the outset, you’re better off working with resources that use a programming language you already know.

Visualizations will help you understand how data structure and algorithms works. You can find awesome animations of this kind from the site: visualgo.net.

file:img/2019_09_12_learn-data-structures-and-algorihtms-visualiazation.png

Learning basic data structures

Some data structures and algorithms, such as bipartite graphs, maximum streams etc, are less intuitive than others. All general algorithms are useful tools, but you rarely need to use these in day-to-day development. If you stick to learning the more intuitive and general algorithms first, you’ll eventually be able to master more difficult, niche techniques.

I recommend these eleven basic data structures to start:

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

Learn how they work, how they are implemented, their common APIs, and how they perform in terms of big-O complexity.

Additionally, GeeksforGeeks has a bunch of good practices for using data structures.

Learning basic algorithm design patterns

From my experience, when studying algorithms, trying to memorize the steps and implementation details often isn’t the best strategy.

[bctt tweet=”If you don’t understand how it works, chances are you won’t remember the particulars anyway; when it comes to algorithms, general understanding is what counts.”]

Don’t try to remember every last detail about the implementation you read about. Instead, try to reinvent, reason and reimplement the algorithm by yourself.

Here’s my preferred workflow for learning algorithms. Repeatedly following it will give substantial results.

file:img/learn-datastructures-and-algorithms.png

Repeat this procedure will make you learn more effectively.

Below basic algorithms are the ones most commonly used. By following the principle of deliberate practice, you should focus on one specific category of algorithms at a time, and try to understand them in order.

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

There are harder problems that need tweaking or require you to combine several algorithms for a viable solution. You should try them after mastering the basics.

Learn by doing

Several websites can help with deliberate practice of data structures and algorithms. I recommend these two, which both have excellent online judging systems:

  • Leetcode: which details almost all common interview questions asked by the big companies (such as Facebook, Google, Amazon); a very helpful resource for interviewing.
  • HackerRank: which has very clean categories for data structures and algorithms, and offers lessons in mathematics, database, and security. It also lets you enter programming contests for fun.

    Another good way to practice is to create trivial projects that use one specific data structure or algorithm.

    For example, this project uses the union-set data structure to create a maze, and also tries to implement pathfinding algorithms. Finishing small projects of this sort will build your confidence, and also teach you its use for creating real applications.

Go deeper

More books

Introduction to Algorithms, 3rd Edition is a great choice for in-depth study. The section on algorithmic reasoning is awesome, and it also offers more details on complexity analysis and mathematical tools.

The other book I would recommend is The Art of Computer Programming. Few programmers have read it from cover to cover, but it remains perhaps the ultimate and authoritative in-depth reference on the subject. It covers almost all facets of programming, and its section on data structures and algorithms is second to none.

Learn from real projects

Knowledge and skill only matter when applied to tangible products.

For instance, you may be curious about how Google’s search suggestions work in terms of data structures or algorithms.

file:img/2019_09_12_learn-data-structures-and-algorihtms.org_20191229_091704.png

Well-crafted open-source projects are incredibly valuable learning resources.

Redis demonstrates the use of many commonly used data structures, and has many performance-related optimizations. The Linux Kernel relies heavily on data structures like linked list, red-black trees, hashes, etc., and fine-tunes its implementations in various ways.

You are not expected to master these things instantly. But after years of practice, you will discover that data structures have become your abstraction tools of choice, and that fitting data structures to custom algorithms has become a comfortable means of solving problems.

Be curious and keep practice!

Join my Email List for more insights, It's Free!😋