DSA Introduction
A data structure is a way of organising data in memory so that the work you need to do with it is easy and fast. An algorithm is a precise, step-by-step method for solving a problem. Together they are called DSA, and they decide whether your program answers in a blink or keeps the user waiting.
In this lesson you will see what DSA means with small, runnable examples, why companies test it in job interviews, and how this course is organised. Every example is in Python 3.14 and runs in your browser.
Data structures and algorithms in daily life
You already use both ideas without naming them. A class register sorted by roll number is a data structure: because it is sorted, the teacher can open it near the middle and find roll number 37 quickly. The way she finds it, "open the middle, then go left or right", is an algorithm.
Software is full of the same pairs:
| Task you see | Data structure | Algorithm |
|---|---|---|
| Undo in a text editor | Stack | Push each change, pop to undo |
| Railway ticket waiting list | Queue | First come, first served |
| Contact search on your phone | Trie or sorted list | Prefix search |
| Map app finding the fastest route | Graph | Dijkstra's shortest path |
| Checking if a UPI ID is already registered | Hash table | Hashing |
The right structure makes the algorithm simple. The wrong one makes even a clever algorithm slow.
Two ways to find a roll number
Here is a sorted list of roll numbers. The first function checks every item one by one, which is called linear search. The second keeps cutting the list in half, which is called binary search. Both functions count how many items they look at, so you can compare the amount of work.
def linear_search(items, target):
steps = 0
for index, value in enumerate(items):
steps += 1
if value == target:
return index, steps
return -1, steps
def binary_search(items, target):
low, high = 0, len(items) - 1
steps = 0
while low <= high:
steps += 1
mid = (low + high) // 2
if items[mid] == target:
return mid, steps
if items[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1, steps
roll_numbers = list(range(1001, 101001)) # 1,00,000 sorted roll numbers
print(linear_search(roll_numbers, 99000))
print(binary_search(roll_numbers, 99000))(97999, 98000) (97999, 17)
Both functions found the same position. Linear search looked at 98,000 items; binary search looked at 17. Binary search only works because the list is sorted, which is the data structure doing half of the job. You will study it properly in Binary Search & Its Variants.
Same problem, different structure
A data structure can change the speed of a program even when the algorithm looks the same. Suppose a coaching centre in Kota wants to know which of today's students have also joined the weekend batch. The first version keeps the weekend batch in a list; the second keeps it in a set, which is built on a hash table.
weekday = [f"S{n}" for n in range(1, 2001)]
weekend = [f"S{n}" for n in range(1500, 3501)]
# Version 1: weekend batch in a list
checks = 0
common = 0
for student in weekday:
for other in weekend: # 'in' on a list scans item by item
checks += 1
if other == student:
common += 1
break
print("List version:", common, "common,", checks, "comparisons")
# Version 2: weekend batch in a set
weekend_set = set(weekend)
common = sum(1 for student in weekday if student in weekend_set)
print("Set version:", common, "common,", len(weekday), "lookups")List version: 501 common, 3125250 comparisons Set version: 501 common, 2000 lookups
Both versions give 501 students. The list version did over thirty-one lakh comparisons; the set version did 2,000 lookups, each of which takes about the same short time no matter how big the set is. You will learn why in Hash Tables.
Why DSA matters for jobs
Most software companies, from start-ups in Bengaluru to large product companies, run a coding round in their interviews. You get a problem, such as "find the first repeated order ID" or "find the cheapest route between two warehouses", and you must write correct code and explain how fast it is. Interviewers use DSA because it shows how you think:
- Can you break a problem into steps?
- Do you know which structure fits, and why?
- Can you say how the running time grows as the data grows?
- Do you test edge cases such as an empty list?
Beyond interviews, DSA helps in real work. A report that takes ten minutes instead of two seconds is usually a data structure problem, such as a list used where a dictionary was needed.
How this course is organised
The course goes from basics to advanced topics, and each lesson builds on earlier ones:
- Measuring speed with Big-O notation.
- Linear structures: arrays, strings, linked lists, stacks, queues and hash tables.
- Recursion and backtracking.
- Sorting and searching, plus the two-pointer and sliding window patterns.
- Trees, heaps and tries.
- Graphs: BFS, DFS, topological sort, shortest paths and spanning trees.
- Dynamic programming, greedy algorithms, bit tricks and range-query trees.
- An interview problem-solving framework and a final project.
You should already know basic Python: variables, loops, functions, lists and dictionaries. If you need a refresher, see the Python tutorial, especially Lists and Dictionaries.
Python, Java and C++
The ideas in DSA are the same in every language, only the names change. This course uses Python because the code is short and you can run it here. Each lesson also notes the matching Java and C++ names, because many interviews let you choose a language.
| Idea | Python | Java | C++ |
|---|---|---|---|
| Dynamic array | list | ArrayList | std::vector |
| Hash table | dict, set | HashMap, HashSet | std::unordered_map, std::unordered_set |
| Double-ended queue | collections.deque | ArrayDeque | std::deque |
| Priority queue | heapq | PriorityQueue | std::priority_queue |
For example, the set check from the earlier example looks like this in Java:
Set<String> weekendSet = new HashSet<>(weekend);
boolean joined = weekendSet.contains("S1600");Common mistakes
- Memorising solutions. Interviewers change the problem slightly. Learn the pattern and why it works, not the exact code.
- Ignoring the size of the input. Code that is fine for 100 items can be far too slow for 10 lakh items. Always ask how big the data can be.
- Writing code before thinking. Spend the first few minutes on examples and a plan. You will see a full method in Interview Problem Solving.
Key points
- A data structure organises data; an algorithm is a step-by-step method that works on it.
- The right data structure often matters more than clever code.
- Binary search on sorted data needs far fewer steps than checking every item.
- A set or dict answers "is this present?" without scanning everything.
- Interviews test DSA because it shows problem solving, not just syntax.
- The same structures exist in Java and C++ under different names.
Exercise
A shop in Pune keeps 50,000 bill numbers in a sorted list, from 5001 to 55000. Use the linear_search and binary_search functions from this lesson to search for bill number 54321 and print the number of steps each one takes. Then search for 60000, which does not exist, and print the steps again.
Show solution
def linear_search(items, target):
steps = 0
for index, value in enumerate(items):
steps += 1
if value == target:
return index, steps
return -1, steps
def binary_search(items, target):
low, high = 0, len(items) - 1
steps = 0
while low <= high:
steps += 1
mid = (low + high) // 2
if items[mid] == target:
return mid, steps
if items[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1, steps
bills = list(range(5001, 55001))
print(linear_search(bills, 54321), binary_search(bills, 54321))
print(linear_search(bills, 60000), binary_search(bills, 60000))(49320, 49321) (49320, 16) (-1, 50000) (-1, 16)
A missing item is the worst case for linear search, because it must look at all 50,000 bills before giving up. Binary search still needs only about 16 steps.
Quick Quiz
1. What is a data structure?
The step-by-step method is the algorithm; the data structure is how the data is arranged.
2. Why can binary search check so few items?
Sorting lets binary search throw away half of the list after every comparison.
3. Which Python structure answers "is this value present?" quickly for large data?
A set uses a hash table, so a membership check does not scan every item.
4. What is the Java name for Python's dynamic list?
ArrayList is Java's resizable array, like Python's list.
Learn DSA with a mentor
Free lessons get you started. Our Java, C & C++ Programming training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
