Welcome to Data Structures & Algorithms! 🚀
You may have heard people call DSA "the hard part" of programming. It isn't magic, and it isn't only for interviews. It is simply the study of two questions every programmer faces:
- How should I store my data?
- What steps should I follow to solve my problem?
By the end of this lesson, you will understand:
- What a data structure is
- What an algorithm is
- Why choosing the right one matters so much
- The most common data structures you will meet in this masterclass
Every code example in this masterclass is available in Java, Python, C and JavaScript. Pick your language from the tabs above any example. Your choice switches every example on the page and is remembered for next time. New to programming? Start with the Java or Python masterclass first.
What Is a Data Structure?
A data structure is a way of organising data so that it can be used efficiently.
You already use data structures in everyday life without calling them that:
- A shopping list keeps items in order, one after another.
- A stack of plates lets you take only the top plate.
- A queue at a ticket counter serves the person who arrived first.
- A phone's contact list lets you jump straight to a name.
Each one stores things, but each one is good at a different job. Programming works the same way.
What Is an Algorithm?
An algorithm is a step-by-step set of instructions for solving a problem.
A recipe is an algorithm:
Boil water → Add tea leaves → Add milk and sugar → Strain → Serve
The steps are clear, they happen in a fixed order, and they always produce the same result. In programming, an algorithm might be the steps to sort a list of marks, find a student's name, or calculate the shortest route between two places.
Data structures and algorithms always work together. The data structure decides how data is stored, and the algorithm decides what to do with it. Choosing one affects how well the other works.
Why Does This Matter?
Imagine your college has 100,000 students (1 lakh), and you need to check whether "Meera" is enrolled.
If the names are stored in a plain list, the computer may have to check them one by one:
import java.util.List;
public class Enrollment {
static boolean isEnrolled(String name, List<String> students) {
for (String student : students) { // look at each name in turn
if (student.equals(name)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
List<String> students = List.of("Aarav", "Diya", "Kabir", "Meera", "Rohan");
System.out.println(isEnrolled("Meera", students)); // true
}
}With 5 names, this is instant. With 100,000 names, the computer might check 100,000 entries before giving up on a name that isn't there.
Now store the same names in a set, a data structure designed for fast lookups:
// add at the top of the file:
import java.util.HashSet;
import java.util.Set;
// then, inside main:
Set<String> enrolled = new HashSet<>(students);
System.out.println(enrolled.contains("Meera")); // true, found almost instantlySame data. Same question. A different data structure, and the lookup no longer needs to check every name.
This is the whole point of DSA: the same problem can be solved slowly or quickly depending on how you store the data and which steps you choose.
The Library Analogy
Imagine a library with 50,000 books, and you want to find one particular book. How long it takes depends entirely on how the library is organised.
1. A Pile on the Floor
Every book is thrown in one big pile. To find your book, you pick up books one at a time until you get lucky. This is like searching an unsorted list: slow, and it gets slower as the pile grows.
2. Shelves Sorted by Subject
Books are arranged on shelves by subject and then alphabetically. You walk straight to the Computer Science shelf and scan only a small section. This is like a sorted structure: you can skip most of the books.
3. A Catalogue Index
The library has a catalogue: look up the title, and it tells you the exact shelf and position.
This is like a hash table (a HashMap in Java, a dict in Python, a Map in JavaScript): you jump directly to the answer.
The books are the same in all three libraries. Only the organisation changed, and that changed everything.
The Data Structures You Will Learn
Here are the most common ones. Don't try to memorise them now; each gets its own chapter.
| Data Structure | Everyday Comparison | Good For |
|---|---|---|
| Array / List | A row of numbered lockers | Storing items in order, fast access by position |
| Linked List | A treasure hunt, each clue points to the next | Inserting and removing items easily |
| Stack | A stack of plates | "Undo" features, matching brackets |
| Queue | A ticket counter line | Handling tasks in arrival order |
| Hash Table | A library catalogue | Very fast lookups by name or key |
| Tree | A family tree | Hierarchies, fast sorted searching |
| Graph | A city map with roads | Networks, routes, connections |
How to Approach This Masterclass
DSA becomes much easier when you follow three habits:
- Picture it first. Draw the data on paper before writing code.
- Trace it by hand. Walk through the steps with a tiny example, like 3 or 4 items.
- Then code it. Once the idea is clear, the code is usually short.
In the next lesson, we will learn Big-O notation: the language programmers use to say how fast an algorithm is.
Remember This
If you remember only one thing from this lesson, remember: A data structure is how you store data, an algorithm is how you use it, and choosing them well is what makes a program fast.