Professional Documents
Culture Documents
3 - Why Learn Data Structures and Algorithms
3 - 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
This article tells about the Data structures.
This article tells about Uses of data structures
This article tells about connecting algorithm to data structures.
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 omelette, playing rugby, checking primes, reading this blog.
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.
From washing machines to self-driving cars to every deterministic action ever taken
can be expressed as algorithms.
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”.
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?
I could go on about binary search and algorithms, but you get the idea… algorithms
run the world!
A library is a data structure. It can store books by their metadata, such as the genre.
For what it’s worth, any database of records is inherently a data structure.
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.
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.
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.
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 trade-offs for
different situations to be able to create optimized solutions.
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.
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 trade-offs and always intend to pick the most efficient one for a
particular problem.
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.
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.
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
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.