Why Learn Data Structures and Algorithms?

Learn via video course

DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
By Jitender Punia
Free
star4.9
Enrolled: 1000
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
Jitender Punia
Free
4.9
icon_usercirclecheck-01Enrolled: 1000
Start Learning

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 omelet, playing rugby, checking primes, reading this blog.

See if the following conversation sounds familiar


Why to learn data structures and algorithms

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!

Data Structures and Algorithm

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.

Learn Data Structure and 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”.

Data Structures and Algorithm-lossless compression

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.

Use of Data Structures and Algorithm

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.

Structures and Unstructured Data

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.

First in first out list in Data Structure

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: Sorting Algorithms in Data Structure

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.

Insertion Sort Scaler in Data Structures and Algorithm

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.