Why learn Data structures and Algorithm
Tags: python, javascript, datastructures, algorithms
Published: 2024-02-03 16:27:55

What crosses your mind when the term "data structures and algorithms" (DSA) is mentioned? Let me take a guess – FAANG(Facebook, Apple, Amazon, Netflix, Google) interviews, perhaps? Spot on. Chances are, that's exactly why you are here. DSA may appear intimidating, leaving you questioning its significance. However, in today’s world, where efficiency is key and competition is fierce, mastery of DSA has become vital. Maybe you have been immersed in coding for years without needing it, and then suddenly, an interview arises, demanding you to quickly acquire DSA skills. Alternatively, it could be a pivotal course in your Computer Science curriculum, or there might be another reason altogether. 

In this article, we will cover:

1. What is a data structure?
2. What is an algorithm?
3. Big-O time and space efficiency.
4. Why should you learn DSA?
5. How to learn DSA.

## What is Data Structure?
A data structure is a way to store and organize data to allow us to efficiently access and modify the data. Operations performed on this data include adding information, searching for it, or removing it.

Let's consider a real-world example. Picture yourself at a library needing a book on Organic Chemistry. You locate the Chemistry shelf, but it contains all sorts of books unrelated to Chemistry.
Because the shelf is disorganized, you spend a longer time searching for the book, which might eventually lead you to check other shelves as well because, who knows, it could be on the linguistics shelf.

Now, envision an organized scenario. You reach the science shelf, find a section for Chemistry, and within that, distinct sections for Organic Chemistry. Suddenly, your search becomes effortless.

In computer science, how we organize data is crucial for easy retrieval. Choosing the appropriate data structure can significantly improve code efficiency.

### Examples of Data Structure 

- **Arrays/Lists**: Ordered, mutable collections.
- **Dictionaries/Objects/Maps**: Unordered, mutable key-value pairs.
- **Sets**: Unordered, mutable collections of unique elements.
- **Tuples**: Ordered, immutable collections.

### What is an Algorithm? 
An algorithm is a set of instructions to perform a computation. Simply put, it involves operations on different data structures along with a set of instructions for executing them.

Let's consider a real-world example.

Imagine you want to teach your five-year-old brother to draw a circle.

1. Put a small pencil in the compass.
2. Align the pencil in the compass to ensure its tip matches the width of the divider.
3. Measure the radius on a ruler.
4. Place the divider on the paper and move it in a circular motion until it forms a circle.

There you have it – you have just implemented an algorithm.

Now, let's transition to a programming example:
Imagine you are to write a program to calculate the sum of two numbers.

1. Define two variables to represent the two numbers.
2. Use an arithmetic operation to add the two numbers.
3. Return the sum.

Python Example

```
def find_sum(num1, num2):
    sum = num1 + num2
    return sum

if __name__ == '__main__':
  sum = find_sum(5, 7)
  print("The sum is:", sum)
```

JavaScript Example 

```
function findSum(num1, num2) {
    let sum = num1 + num2;
    return sum;
};

// Example usage:
let sum = findSum(5, 7);
console.log("The sum is:", sum);
```

Let's discuss the space and time efficiency of an algorithm.

### What is Space and time efficiency?
Space and time efficiency is concerned with how the algorithm utilizes two essential resources: space and time.

- **Space**: This refers to memory and storage considerations.
- **Time**: This refers to the speed at which the algorithm operates.

In the next tutorial, we will discuss Memory where we will dive into this deeper.

## What is Big-O Notation?
Big-O notation can be used to describe an algorithm's space and time efficiency. It provides a set of rules to measure both space and time complexities.

Let's consider two functions.
A: Sort(): Modifies the original array and returns an ordered array.
B: Sorted(): Creates a new sorted array without modifying the original iterable.

Given solutions A and B, which one is more efficient in terms of space?

Answer: A is more efficient in terms of space because it uses minimal memory.
B needs more space to hold the new sorted array.

Now that we have clarified the key terms, let's proceed to answer the question.

## Why learn DSA?
**Improve Problem-Solving Skills**: This is a fundamental skill for programmers. DSA teaches you to think, rapidly improving your problem-solving ability.

**Help in Solving Real-world Problems**: DSA assists in selecting the right data structures and algorithms to tackle real-world issues. Consider a social network or map; a graph algorithm can be instrumental in solving such problems.

**Write Optimized Code**: DSA improves efficiency in terms of both space and time. Optimized code runs faster, utilizes less memory, and is overall more efficient.

**Prepare for Technical Interviews**: Leading companies like FAANG often ask questions related to DSA in technical interviews. Familiarity with DSA concepts becomes crucial in attaining success in these interviews.

I know this has been a lot to grasp so far. Now, you might be wondering how to start and what resources you need. These questions will be answered shortly.

## How to learn DSA?

1. Be comfortable in a programming language e.g Python, JavaScript, C, Java, etc
2. Start with searching and sorting algorithms because it is much easier to understand before diving into complex algorithms.
3. Understand, don't memorize; visualize and write pseudocode.
4. Scrabble algorithm on paper.
5. Watch various videos for implementation insights.
6. Apply algorithms in real-world projects, reflecting on past coding challenges.
7. Consider the big-O space-time complexity to determine the efficiency of your code.

## Resources 

- [Python-DSA](https://youtube.com/playlist?list=PLzgPDYo_3xukPJdH6hVQ6Iic7KiJuoA-l&si=RkokOtJVsztPs5zR) & [Sorting algorithms](https://youtube.com/playlist?list=PLzgPDYo_3xunyLTJlmoH8IAUvet4-Ka0y&si=bse_WbsqXnnqscxS)
- [Javascript](https://youtube.com/playlist?list=PLC3y8-rFHvwjPxNAKvZpdnsr41E0fCMMP&si=tpr1XiQN4qXIQGP_)
- [Java](https://codewithmosh.com/p/data-structures-algorithms)
- [Interview Preparation](https://codewithmosh.com/p/interview-preparation-bundle)
- [Leetcode](https://leetcode.com/)
- [Log2base2](https://log2base2.com/dsa)

## Summary
Data Structures and Algorithms are key concepts in computer science. It can help you write efficient code and pass interviews.

Check out the next article where I delve into DSA in-depth.

Until then, for more insights:
☕ If you enjoy my content, you can buy me a cup of coffee [here](https://www.buymeacoffee.com/ifeoluwapru).
📰 To read more from me, subscribe to my newsletter.
🧸 Check my social media profiles: [Twitter](https://x.com/PraiseID3?t=WqgSov3o-htz89CukDu3oA&s=09), [Github](https://github.com/praise002), and [LinkedIn](https://www.linkedin.com/in/praise-ifeoluwa-idowu-back-end-developer-django?utm_source=share&utm_campaign=share_via&utm_content=profile&utm_medium=android_app).