Sarveshwar Shukla

Data Structures and Algorithms Made Easy

Data Structures is a way to arrange data, and algorithms are the sequence of steps we would take to solve a given problem using those data structures. Data Structures and Algorithms (short for DSA) are an essential part of a programmer’s life; whether you are a Competitive Coder or a keen Developer, Data Structures are a must-learn thing. For competitive coders, it will help them in their problem-solving approaches. At the same time, it will make developers write scalable and maintainable applications using the most appropriate form of data and strategy.

What is Data Structure?

Data Structures and Algorithms made easy. Data Structures are a collection of values; these values can have a relationship among themselves and have functions associated with them. There are various types of data structures in programming, and each one is different in what it can do and what it is best used for, i.e., each data structure is good and is specialized in its own way for which it can be used.

Data Structures can be visualized as any compartment or container; consider it like a file cabinet or a folder of different types. Let’s understand this with the help of an example –

DATA STRUCTURE AS COMPARTMENTS

Here we can see we have some containers, each of which is used for a specific purpose.

  • Storage box – We use this for storing things; shipping companies also use them to ship products etc.
  • Kitchen containers – They are generally used to store spices, pulses, biscuits in the kitchen, etc.
  • Cupboards – Cupboards are primarily used to store clothes, books, other essential things, etc.
  • Water Bottle – Water bottles are generally used to store water, health drinks, etc.

So, all of these containers are used to store certain specific things and are best at that. For example, it will not be logical to use a water bottle to store spices and pulses. Similarly, it will not be logical to store clothes in a kitchen container. Meaning each of these containers is designed to hold a specific kind of thing or value, and that’s precisely what Data Structures are; it’s a way for us to organize data. So that we can go to the cupboard and retrieve that data quickly, we can go to the kitchen box and have the biscuits efficiently and promptly. We can take things from data structures, and we can put things in data structures, and that’s all it is.

Like in real life, we have all these data structures – storage boxes, kitchen containers, refrigerators, cupboards, water bottles, etc.- to store different things; similarly, in programming, we have a lot of data structures to store different types of data. The most commonly used ones are –

  • Arrays
  • Strings
  • Linked Lists
  • Stack
  • Queue
  • Tree
  • Graphs
  • Hashmap
  • HashSet etc.

Technically, int, float, char, booleans, etc. also store data, and they can be considered a Data Structure, but more appropriately, they should be called a DataType. This is because a datatype is the most basic and most common classification of data; it is through which the compiler gets to know what type of information that specific variable is going to hold, whereas a data structure is generally a collection of different forms of different datatypes that are created to fulfill a particular need, like arrays, linked lists etc.

In simple words, Data Structures are simply a way to arrange data in the main memory (RAM) for efficient usage, i.e., when we need the spices, we know exactly where we have to look (the kitchen box), and when we need to put some spicies, we know where it is best to keep that (the kitchen box) so that we can access it faster when needed. This means Data Structures are a way to store, process, retrieve, and organize data in the main memory, ie. RAM. Talking about varieties, there are various types of Data Structures that we have today, and each of them is designed to fulfill a specific need. It is essential to know that data structures are being used in almost every program or application that was and is getting developed today; and needless to say, it is the fundamental of modern Computer Science and Software Engineering.

What is an Algorithm?

Data Structures and Algorithms made easy. An Algorithm means the sequence of steps that we would take to solve a given problem, i.e.; it is the step-by-step procedure that lists out a set of instructions that needs to be executed in a defined order to get the desired output. While there is no specific template for writing algorithms, we can look at certain factors that comprise a good algorithmic approach.

  • Precisely defined inputs and outputs.
  • Every step in that algorithm should be clearly defined.
  • That Algorithm should be one of the most effective ways to tackle that problem etc.

Let’s understand algorithms with the help of a real-life example – Suppose we are at home and we need to prepare tea; for that, we would be taking some sequence of steps like –

  • Take out the pan and pour enough water into it.
  • Light the Gas and place the pan over it.
  • Pour the necessary amount of tea leaves, sugar, and ginger into it (you may add anything if you want).
  • Wait for some time and then pour a necessary amount of milk into it.
  • Let it boil and serve after it’s ready.
  • Drink the tea.

You see, that’s how we make tea, right? So congratulations, you understood what an algorithm means using a real-life scenario. That is precisely what an algorithm is; we had a problem (we wanted to drink tea), and to solve that, we took several sequential steps and found the solution (made our tea).

Obviously, along with the examples, we should also be aware of the actual technicalities. So in this article, now let’s have a look at that. There may be times when you may feel that understanding a specific thing requires more effort than what you do regularly, but that’s fine; keep going, and it will be easy as you follow along.

ALGORITHMS

Note – An algorithm is not specific to a particular programming language; instead, it is a general sequence of steps to solve a problem, and those steps can later be implemented in any programming language of our choice.

What is the Need to Learn Data Structures and Algorithms?

Let’s understand this with the help of an example.

We all have written the program to find the nth Fibonacci number in the Fibonacci series. The simplest and the most common approach that anyone will follow while starting with programming will look like this.

int fibonacciBasicApproach(int num)
{
    if (num <= 1)
    {
        return num;
    }

    return fibonacciBasicApproach(num - 1) + fibonacciBasicApproach(num - 2);
}

While this may sound fine to the starters, and they are not wrong, eventually, this code has several problems. First, if the passed input num gets really large, then the computer could even take days, months, and maybe years to compute the output for that particular index. This is because the time complexity of this specific approach or, let’s say, this particular algorithm is exponentially increasing with the value of num.

But, we all, when started, wrote the Fibonacci program exactly like this, yes that’s true, and it was fine, too, because, at that time, we were not really concerned about the performance of our code; we were just started learning, we were in the learning phase, so it can be ignored there. But at the end of the day, the code written above is not the best approach to calculate the nth Fibonacci number.

Let’s now have a look at another approach

int fibonacciBetterApproach(int num)
{
    int fibArr[num + 1];

    fibArr[0] = 0;
    fibArr[1] = 1;

    for (int i = 2; i <= num; i++)
    {
        fibArr[i] = fibArr[i - 1] + fibArr[i - 2];
    }

    return fibArr[num];
}

This code is a linear time solution to the same problem, i.e., finding the nth Fibonacci number; here, we have used the bottom-up approach of dynamic programming to achieve the same solution in linear runtime.

Now you see why it is essential to learn and understand algorithms. Programming is not only about writing programs or better, correct programs but also about writing efficient programs and designing efficient algorithms to reach the same solution.

In a nutshell, Appropriate Data Structures + Well-planned Algorithms = Efficient Programs

Why are Data Structures Important?

Knowing a Programming language is good, but more is needed. The real key thing is having a good command of problem-solving strategies, as we saw above, and in layperson’s terms, we got the idea of why learning data structures and algorithms are essential. Now let’s look at a few technicalities of why one should learn Data Structures and Algorithms.

Algorithms are the Core Part of the Program Libraries

In an era where we use library functions and APIs to ease our work of writing programs, softwares, etc., learning data structures and algorithms may seem unnecessary, as it is already coming packed with those library components. But this is precisely a reason why one must learn data structures and algorithms. To better understand the working of those libraries so that we can write compatible code using them, and this also will prevent us from applying those components in a possibly wrong way.

Algorithms are Technology

While designing software or a product, its system performance depends on choosing efficient algorithms as much as it depends on choosing fast hardware. So, for example – if the application needs fast networking capabilities, then the developers would be required to work heavily on network routing algorithms, etc.

Important for Coding Interviews

Why do companies test candidates’ knowledge of Data Structure and Algorithms in the interview process? This is because companies encounter complex and large-scale data-intensive problems in real life. Hence, they would prefer to hire a capable programmer who can solve those problems efficiently in terms of time and memory. Having a stronghold of DSA depicts the programmer’s ability to solve those problems effectively when encountered.

Improves Problem-Solving Abilities

Data Structures help us improve our problem-analyzing and problem-solving abilities; using them, we can find such straightforward ways to perform tasks that wouldn’t have been possible if we didn’t know the DSA concepts. Here is one of the best quotes by Donald Knuth “People who analyze algorithms have double happiness. First, they experience the sheer beauty of elegant mathematical patterns surrounding elegant computational procedures. Then they receive a practical payoff when their theories make it possible to get other jobs done more quickly and economically.”

How to Learn Data Structures and Algorithms?

Learning Data Structures and Algorithm is not a difficult task, but it demands a well-structured path, consistency, and regular practicing of problems. Let’s now look at the approach one can follow to get started with DSA.

Getting to Know about the Topics

It is the most vital step. Before getting started into the learning, we should first understand what topics we have to cover and the best resources to learn them.

Dividing into sections, the first one being Important Data Structures and the second section being Most Important Algorithms; on categorizing, we would have

Most Used Data Structures

  • Arrays
  • Strings
  • Linked Lists
  • Stack
  • Queue
  • Tree
  • Graphs
  • Hashmap
  • Hashset etc.

Most Used Algorithms

  • Searching Algorithms
  • Sorting Algorithms
  • Hashing Algorithms
  • Prefix Sum
  • Suffix Sum
  • Recursions
  • Backtracking
  • Tree Traversals
  • Graph Traversals
  • Dynamic Programming
  • Advanced Algorithms on Trees and Graphs etc.

Finding the Best Resources to Learn those Topics

Once we know what to learn, we now have to think about where to learn those topics. For that

  • One may follow standard books to learn Data Structures, but that will be kind of not so good way to start; books are undoubtedly one of the best resources to know anything, but what I will suggest is to go through books once your concepts are somewhere on a reasonable level, books have lots of detailed pieces of information no doubt, but that may not be a very appropriate start for a beginner.
  • Second way, one may follow online tutorials, courses, and video lectures to understand these topics; there are plenty of free resources on youtube to ace your understanding of Data Structures and Algorithms. Along with that, there are also well explained and detailed articles explaining almost every topic mentioned above in an easy-to-understand way and hence following the idea of Data Structures and Algorithms made easy.

Individuals are free to follow whatever they prefer and can start learning the DSA.

Understanding the Fundamentals Properly

We now know what we have to learn and where we can learn that. So now, we can start our DSA journey, but before proceeding, I do want to mention one of the most significant steps in this journey is understanding the fundamentals and internal workings of an approach or an algorithm. Without learning the fundamentals, we would not be able to relate to them properly, and hence it would be tricky if we would want to go to their advanced implementations.

Moving into the Depth

Now we know the basics and are well aware of what data structures are and how they function. It is now the time to start learning data structures in depth, going into the minute details of topics, learning more complex algorithms, and optimizing the performance of our programs.

Consistency is the Key

Now, after this, all that is required for us is to be consistent, drop by drop of water fills the ocean. Practice every day, participate in coding contests that are organized almost every week on various platforms like leetcode, CodeChef, etc., brush up on your problem-solving approaches, learn new topics, and, most importantly, stay consistent.

Important Points to Master DSA

1. Regular Revisions

It is often heard among programmers that they solved a problem today, and they do not remember it after 9-10 days, and if the problem comes to them after a week or two, most of them find themselves needing help with what to do. This is because they keep solving without ever coming back to revise and re-understand what they previously learned. The solution is revisiting what we have learned at regular intervals until we completely understand them. For example, try revising what you have learned every day for a couple of days, then make it seven days, then slowly shift it to one month, and you will see you get it.

2. Practicing Regularly

Knowing data structures alone is insufficient; now, we have to improve our speed and efficiency, i.e., how quickly we can find an optimal solution for a problem with minimum chances of having an error. And the only way to achieve this is Practice, Practice and Practice. Various online platforms offer a variety of questions based on the type of data structure, difficulty level, etc. one can practice questions there; along with that, participating in live coding challenges would help an individual find the solution to problems in a bounded time frame.

3. Solving a Variety of Problems

A question will not always be from a few selected topics; there are much more things to learn. Practice new problems, learn new things, and increase your difficulty level; this will not only help you tackle the problem statements but also will widen your thinking capacity about the application and implementation of different algorithms.

4. Participating in Coding Contests

At this stage, we might think that we are now good at DSA, but is it true? The only way to find out is to test yourself against other programmers. As mentioned above, various online coding platforms host coding contests almost every week. Try to participate in that; it will not only ground test your knowledge but also will bind you into a limited time frame to find an optimal solution. This will also familiarize you with the interview environment of coding rounds, as we are also time bound there. And always try to solve the problems you were unable to solve during the contest and understand their approach.

How Long Does it Take to Learn DSA?

Learning is never ending process; we learn throughout our life. While for this question, how long will it take to learn DSA? There is no specific answer, as it depends on person to person. One individual may require one month to master some concepts, while another may need some additional months to follow the same. But if we talk on a general basis and try to answer how long it will take for a person to complete the DSA basics? Then it would be around 10-12 weeks if done consistently for about 2-3 hours or more daily. Again, it highly depends on a person’s potential and eagerness to learn.

What are the Best Resources to Learn DSA?

As mentioned above, there are various ways to learn any specific thing. We have the internet today, and there are a countless number of resources that can be used to learn any skill that one can think of. Talking specifically about DSA, then one may refer

  • Standard Books
  • Youtube tutorials (there are some excellent channels explaining each and every topic of Data Structures in detail)
  • Online Articles, you may read The complete Data Structures Tutorial by Scaler Topics which primarily focuses on the idea of Data Structures and Algorithms made easy.

Conclusion

  • Data Structures are a way to arrange data in the main memory for efficient usage.
  • Algorithms are the sequence of steps that we would take to solve a given problem using those data structures.
  • Data Structures and Algorithms (DSA) are essential for everyone. Be it a competitive programmer, a software developer, or just a programming enthusiast, it is a must-learn and understand thing.
  • Data Structures and Algorithms are at the core of the technology that we use today. Every kind of system getting developed relies heavily on efficient usage of data structures and implementation of algorithms for their working.
  • DSA concepts not only improve our problem-solving and thinking abilities but also are essential for coding interviews.
  • To start learning DSA, one should first try to know about the topics that they need to cover, then the resources from where they can understand that, and then consistent practicing.
  • Focus on understanding the fundamentals than just memorizing solutions.
  • Learning is a lifelong journey.

FAQs

Q. What is a Data Structure?

A. A data structure is essentially a logical way, to organize data into the program.

Q. What is an Algorithm?

A. An algorithm is a sequence of certain steps that we would take while solving a problem.

Q. Why are Data Structures and Algorithms important?

A. As we discussed above, the example to find the nth number in the Fibonacci series is that having a knowledge of what would be the best way to store some data in a program, and how should the approach be planned, can improve the efficiency of the code by several times, these are some of the reasons why one should know data structures and algorithms.

Q. Where to start learning Data Structures and Algorithms

A. There are various online tutorials from where you can start. While this could also be a good start. The complete Data Structures and Algorithms made easy series by Scaler Topics

Author