Damn Data structures and algorithms

What are they and why do they matter??

·

4 min read

I thought I was there...

I remember during my internship back in 2019 I was asked to write this code that takes files from a directory and sends them as emails based on an XML file. There are times when a server would be down and the point of this was to notify those in charge of the servers to turn it back up again. It took me about 2 weeks to complete and of course, it was satisfying. It felt like I could build anything from just basic programming knowledge. I was anxiously waiting for the next project to work on. That was the case until I recently got humbled by this "easy" LeetCode question.

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

Hmm...

Seems easy right?

Here's what my code looked like...

class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        for i in range(len(nums)):
            pivot = nums[i]
            for j in range(i+1, len(nums)):
                if pivot == nums[j]:
                    return True

        return False

Easy. Seems like what most beginners would have gone with (as far as they can understand the code)

So this bad boy passed all the tests but when I submit the code, this is what I see

image.png

It turns out that LeetCode has a low tolerance for slow algorithms. Also, the default test cases are not the only test cases on which your code would be executed. Other test cases are given to LeetCode by other users to test the efficiency of your program.

Now, I had heard about data structures and algorithms during my university years but I didn't see the need for them until now. I realized that I needed to learn more about these topics in order to write efficient code.

Okay. What are these data structures and algorithms that you speak of?

A data structure is a way to organize data in a virtual system. Consider number sequences or data tables: both are well-defined data structures. An algorithm is a set of steps performed by a computer to change an input into a desired output.

Together they allow programmers to build whatever they like in possibly any programming language while ensuring efficient and optimized written code. Because once you understand DSA, it is applicable in a variety of programming languages

You already know a data structure already. Arrays. Have you heard of arrays? The array is the very first and most basic topic of the DSA.

So what does this have to do with anything?

Think about it...

Data is everywhere and it is increasing every day. The size of available data in 1922 would have been a lot smaller than that of 2022. Maybe a brute force algorithm would have been the best at that time because there was no other way. If you're going to work for a company, say Google. They deal with zettabytes of data every day (probably...) and to work with that large amount of data, they need efficient solutions. Now you, that want to work with Google would need to know how to create not just solutions that work but efficient ones. Solutions that can work on millions of entries in the least possible time and takes the least possible space (because code takes some space to run) and not just some random array. And when I say the least possible time possible, I mean less than a second. Nope, it's not unrealistic. Just go to Google and search for something.

image.png

Also, You can look through a plethora of IT roles with programming requirements. You're going to need to know DSA. You can't avoid it. You will need it to create efficient solutions. Some would need it more than others but the bottom line is that you can't avoid it and you will need it to create efficient solutions.

Ready to learn?

The algorithm I showed you earlier for the LeetCode question is called a Brute force algorithm. It's the most intuitive algorithm for a beginner.

Let's face it. This is also the most inefficient algorithm out there. So there must be better ones, right? You have to take your time to learn them and practice consistently to recognize patterns in data structures and algorithms. This means you'll have to sign up for LeetCode which is where you can practice your DSA skills.

I know I might be a random blogger you came across on the internet but this random blogger is demanding more from you if you are serious about your developer career.

So here is a least of free resources I found(because you would like to save money. Obviously)

You could go to Youtube and check out

  • FreeCode Camp
  • CS Dojo
  • Programing with Mosh
  • codebasics

Google has a free course on DSA. You can check it out here

If you happen to know any other free resources, please do put hem down in the comments below. I wish you the very best in your developer journey