Getting started, this is the very basis of all competitive coding. This article will provide an overview of what data structures and algorithms are. It lies at the very heart of competitive coding, and future articles will go more in-depth into various data structures and algorithms.
What’s the difference?
Good question! Why do we generally say data structures and algorithms together, and why are they not the same? It’s because there are 2 main concepts at play here.
Data Structure
A tool used to store and arrange data in some form so that it can be easily accessed or manipulated later. Examples include arrays, stacks, trees and matrices.
Algorithm
A tool used to manipulate already arranged data in some way, usually to solve some given problem. Examples include sorting an array, searching a matrix, or traversing a tree.
Wow, that’s a lot to take in, so let’s break it down a bit.
Can you give me something more intuitive?
Sure thing, let’s not memorise definitions here. Let’s take an example.
Problem:
You’re given a bunch of books. You want to be able to find any book from this bunch quickly. How do you go about doing it?
The solution:
Here’s how you’d probably think about the terms we’ve discussed.
The data in this case are the books themselves. Data is basically information we need to process.
The data structure in this case would be a stack of books. A data structure is a way to store data.
The algorithm in this case would be to look through the book stack to find a book. An algorithm is a sequence of steps you take to solve a problem.
Now the more observant reader would notice a better way to solve the problem- why not sort the book stack first? Good job, you’re on the right track! In fact, sorting itself is also an algorithm, and we’ll discuss similar problems to this one in the future.
Okay cool, but how do I use these?
The point of competitive coding is not to test just your coding skills. It is, that given some data, you need to use it to solve a problem quickly. Let’s continue with the above example, which would be the best way to go about the problem?
What if I have a million books and a hundred people asking me to look for a book? Is re-searching the entire stack every time the best way to solve the problem?
Obviously the answer to the above is a resounding NO! There are better data structures we can use that will let us find things faster. There are other concerns too, such as,
What if I need to keep adding and removing books frequently?
What if I only need to search for one book?
Should I divide the stack into smaller stacks to make my work easier?
All the above questions are, in some form or the other, addressed in competitive coding problems, and this is the essence of data structures and algorithms.
Once you get the hang of data structures and algorithms, you will understand the point of competitive coding. Learning this will boost your problem solving skills and even help in coding interviews!
If you enjoy reading this kind of content you should consider subscribing, I’ll be quite grateful of your contribution to Elucidate.
And if you enjoyed this article, do consider sharing it with your friends and encouraging them to subscribe too!