Search for Courses, Topics

# Why Learn Data Structures and Algorithms?

## Overview

Data structures and algorithms (DSA) goes through solutions to standard problems in detail and gives you an insight into how efficient it is to use each one of them. Data structures and algorithms are essential for becoming a skilled programmer since they considerably improve one's problem-solving capabilities.

## Scope

• How to make code scalable.

Have you ever wondered why all the top product-based companies focus so much on data structures and algorithms in your technical interviews for positions like software engineer, data scientist, machine learning engineer, and so on? đ€

Or, have you recently stumbled upon a cat video and were subsequently bombarded with kitten content making you curious about how the youtube algorithm works? đ„°

Or maybe you are just curious and want to explore this interesting field of computer science for your further research? đ§ Either way, itâs always crucial to understand why to learn data structures and algorithms, and how these topics can be considered two sides of the same coin!

## What is an Algorithm?

In laymanâs terms, an algorithm is a step-by-step set of instructions for doing stuff such as making an omelet, playing rugby, checking primes, reading this blog.

See if the following conversation sounds familiarâŠ

Here, John is under the impression that algorithms only pertain to ânamed conceptsâ like breadth-first search, quicksort, or binary search but not to something as simple as finding the sum of first N natural numbers.

Thatâs a fundamental misconception!

Let me repeat, an algorithm is a step-by-step set of instructions for doing stuff. Now, the question of a particular algorithm being an efficient one is entirely different.

Think about it, you have to have some instructions to find that sum of first N natural numbers. It can be either by running a loop and solving the problem in linear time or by implementing the concept of arithmetic progression and solving the same in a constant time.

The latter one is evidently a more efficient algorithm. Simple but an algorithm nonetheless.

Even a simple program like âhello worldâ stands on the shoulders of algorithms, and so does complex software.

In fact, algorithms run the world!

From washing machines to self-driving cars to every deterministic action ever taken can be expressed as algorithms.

Let us look at some examples â

"In One Hand Youâll Have Morpheusâ Life

And In The Other Hand, Youâll Have Your Own.â

The Oracle says to Neo in The Matrix

Not much of a choice when you think about how, during that conversation, she literally installed freshly baked âcookiesâ for Neo to choose what she desires.

Relax, not a spoiler! đ Todayâs recommendation engine tries to achieve the same as The Oracle by providing choices you are most likely to choose. Can you guess how that works? Boom! Algorithms, more precisely, machine learning algorithms.

If youâve used Gmail before, you know that a file size of more than 25 MB is not allowed, and instead, you must opt for a drive link.

Unfortunately, you possess a file of size 47 MB and donât want to send a drive link for some reason. So you compress it, and the zip file turns out to be around 24 MB in size (wish I was that lucky đ)

Ever wondered how that happens without losing any information during the process?

Well, the answer is lossless data compression algorithms! To shed some light, letâs take a string âaaaaaaabbbcdddddddâ of length 18 and a simple compression algorithm that encodes our string to something like âa7b3c1d7â of length 8. Notice how the information about the string is still preserved and can be decoded back. Hence, itâs called âlosslessâ.

Furthermore, đ

In a dictionary, we can search for a word relatively fast as opposed to examining one page at a time. So, what is it that we do to get the word in such a short time?

As a human being, searching in a dictionary is pretty intuitive. But if we were to peek behind the curtain of our own procedure and formulate it, we would call it the âBinary Search algorithmâ. You see, a unique feature of any dictionary is that the words are sorted in lexicographical order. For the uninitiated, starting from the first page and checking until they encounter the desired word is referred to as âLinear Searchâ and this would have been the only approach had the dictionary not been sorted in said order.

Say, we are interested in finding the word âChiaroscuristâ then how can we leverage this sorted property of a dictionary?

Well, we will open the dictionary in the middle and check the first word, if it starts with âkâ, then we know our word lies on the left half since âcâ comes before âkâ. So we have eliminated around the entire right half with one check. We repeat the process for the left half, and eventually, we find âcâ and our word as well.

The reason we call binary search a binary in the first place is because we have two choices at every iteration like up-down, left-right, low-high, and so on. Binary as in two, get it?

Sometimes people confuse this binary search to be a âdivide and conquerâ algorithm. It is not. Although it does divide a problem into smaller subproblems, it doesnât need to conquer those subproblems and can simply ignore them. So, it acts more like a âdivide and ignoreâ algorithm.

I could go on about binary search and algorithms, but you get the ideaâŠ algorithms run the world! đ

Now, shouldnât these robust algorithms always need some form of structured data to work with? Like binary search needed a dictionary!

Hence, the coin flip..

## What is Data Structure, and Why do I Need to Learn It?

Data Structures are nothing but âmeaningfulâ arrangements of data that algorithms can use to solve any particular problem! đ€

A library is a data structure. It can store books by their metadata, such as the genre.

Hospital records are data structures, and so are phone directories.

For what itâs worth, any database of records is inherently a data structure.

These data structures are often categorized into two groups based on their structures (pun intended or not đŹ):

• Linear Data Structure

• Array, Linked Lists, Stack, Queue, etc.
• Non-Linear Data Structure*

• Trees, Graphs, Heaps, etc.

In computer science, we often talk about abstraction and programming to an interface.

For instance, you donât need to know automobile engineering to drive your car to the grocery store. You just have to know how to drive since the manufacturers already abstracted away all the intricacies of the car engine and other internal mechanisms. And for driving, you do get interfaces such as a steering wheel and a gearshift in order to interact with the car.

Likewise, every data structure has a corresponding interface known as Abstract Data Type or ADT.

For instance, a queue is a linear data structure implemented on top of its ADT, and this concretion must maintain the First In First Out (FIFO) ordering of elements. It simply means, the first person to get in the queue, would be the first person to get out of the queue.

This idea of a queue abstract data type might have been inspired by a real-world standing queue in front of a grocery store counter or similar scenarios.

Essentially, a queue must support insertion (enqueuing) at the rear end, and deletion (dequeuing) at the front.

Naturally different languages like Python, C++ will have their own implementation of the queue and a lot more features on top of that ADT.

Simply put, an abstract data type only addresses the interface, and data structures implement that interface.

Although itâs good to know these vocabs, donât stress too much since we mostly talk about data structures and abstract data types interchangeably.

âSo, why do I need to learn these data structures in the first place?â â you ask.

âSimply because it makes our lives easier!â

Can you imagine going to a library and finding all 10,000 books stored randomly? No amount of âAccio bookâ will save you mate! đȘ

A patient is about to have surgery and yet hospital workers are trying to find his/her records in every corner of the 11 storeyed building. Imagine the mayhem and stress and panic! â

Buying a new dictionary only to find the words arenât in a lexicographically sorted order (who does that except to pull a prank? đ€).

I think itâs safe to say the kind of unfortunate situation we would be in had our society be like that. However, since itâs not, then why should our software be?

And thatâs why we need to learn data structures and understand their tradeoffs for different situations to be able to create optimized solutions.

## Connecting Data Structures to Algorithms

Now that we have witnessed both sides of our DSA coin đȘ, suffice to say, an algorithm does leverage data structure to solve problems.

However, neither algorithms nor data structures make sense in isolation e.g. an algorithm along the lines of 1) wear a helmet 2) then ride a bicycle doesnât hold in the absence of a helmet or a bicycle.

Earlier, I addressed a simple lossless compression algorithm for the string âaaaaaaabbbcdddddddâ but notice how the algorithm wouldnât make any sense on its own except for the context of a string.

So far, we only examined the dependency in one direction i.e. can an algorithm be isolated from a data structure. How about the other way? Can a data structure make sense in isolation?

Not necessarily!

You actually need an algorithm to build the data structure in the first place. Even if we give it a pass, and assume it exists!

Now what?

At the very least you need an algorithm to even observe it, let alone modify or work with it!

Thatâs why data structures and algorithms are often mentioned collectively, be it in tutorials, lectures, or courses. Besides, The purpose of data structures and algorithms is to make your software as efficient as possible. Hence, you must be aware of their tradeoffs and always intend to pick the most efficient one for a particular problem.

## Using DSA to Make Your Code Scalable

Letâs take a scenario â your manager asks you to rank customers according to their purchase history, and the higher the purchase, the better the rank should be.

The record of their purchase is present on a CSV file.

After giving some thought, you come up with a feasible solution vis-a-vis sorting the record according to purchase in descending order.

Since sorting goes well with contiguous memory, you first load the data into a suitable data structure like an array that stores related data items in a sequential address space.

Next, you have a plethora of sorting algorithms to choose from:

Letâs say you pick insertion sort and apply the algorithm to the array.

Voila! It works âŠđ„ł

Next thing you know, your team rolls out a new feature that rewards customers based on their ranking which in turn creates positive reinforcement and the sales go through the roof. However, on a blissful Saturday night, your code breaks, and the production is down!

Even though you had solved the problem, it turns out that the insertion sort is pretty costly and makes your program run slower and slower as the size of the customer record becomes larger and larger.

More formally, insertion sort scales quadratically with respect to the data set.

In the end, you are advised to pick a more efficient sorting algorithm such as merge sort and modify your code to handle the scale. Now, letâs hope you donât lose that PPO! đ€đ»

But, these scalability issues can also arise from a memory standpoint where we might have to trade off time for optimizing space.

In that context, an algorithm like quicksort might be a better choice since itâs an in-place sorting algorithm in that it doesnât need extra space in memory to sort unlike merge sort (âextra spaceâ refers to memory apart from the record itself).

The point being, data structures and algorithms play a crucial part in handling the scalability of a software system. Perhaps, that is the reason why companies always ask about DSA problems in an interview. đ€

## Epilogue

âWe need to learn data structures and algorithms since it enhances our ability to solve problems much more efficiently and helps us think through a scenario methodically.â â By someone who regrets not practicing enough DSA back in college

Learning data structures and algorithms can be fun if we try to anthropomorphize these concepts since, for the most part, DSA was inspired by real-world objects and situations, much like other components of software engineering!

And the difference between an okay developer and a good developer comes down to not knowing and knowing the breadth of DSA.

Pardon me for not explaining to you how the youtube algorithm works? But that was never the intention of this article. This article exists to answer why we need such algorithms in the first place!

Our computers are continuously evolving and would require more and more advanced algorithms and storage solutions in the coming days.

Hence, we have to be prepared not just for coding interviews but also to be able to perform well in our job assuming you wanna be a software engineer or a data scientist or a machine learning engineer or anything that benefits from knowing DSA. đ€

## Conclusion

Data structures allow information storage, it provides the means for management of large data like databases, work together and are necessary for efficient algorithms, safe storage of data, allows easier processing of data, and the use of the internet to access data anytime.

Free Courses by top Scaler instructors
Certificates
Data Structures Tutorial
This program includes modules that cover the basics to advance constructs of Data Structures Tutorial. The highly interactive and curated modules are designed to help you become a master of this language.'
If youâre a learning enthusiast, this is for you.
Criteria
Upon successful completion of all the modules in the hub, you will be eligible for a certificate.