← Back to DSA Course | Chapter 1: Introduction & Complexity | Lesson 1 of 6

What is DSA

What is DSA

DSA stands for Data Structures and Algorithms: the two halves of how programs handle information. Data structures decide how you organize and store data, while algorithms are the step-by-step logic that acts on it. Together they're the toolkit behind every efficient program, from a search engine to a food-delivery app's route planner.

Example: What is DSA

#include <iostream>
using namespace std;
int main() {
    int data[] = {2, 4, 6, 8};
    int sum = 0;
    for (int i = 0; i < 4; i++) sum += data[i];
    cout << "Sum: " << sum << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        int[] data = {2, 4, 6, 8};
        int sum = 0;
        for (int i = 0; i < 4; i++) sum += data[i];
        System.out.println("Sum: " + sum);
    }
}
data = [2, 4, 6, 8]
total = 0
for x in data:
    total += x
print("Sum:", total)
#include <stdio.h>
int main() {
    int data[] = {2, 4, 6, 8};
    int sum = 0;
    for (int i = 0; i < 4; i++) sum += data[i];
    printf("Sum: %d\n", sum);
    return 0;
}

Data Structures

A data structure is just an organized way to hold data so specific operations are fast. An array is great for fixed-order access, a linked list for cheap insertions, a hash map for near-instant lookups. Picking the right one for the job is often the difference between a program that scales and one that doesn't.

Example: Data Structures

#include <iostream>
using namespace std;
int main() {
    int marks[] = {90, 85, 78};
    cout << "Stored value marks[0]: " << marks[0] << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        int[] marks = {90, 85, 78};
        System.out.println("Stored value marks[0]: " + marks[0]);
    }
}
marks = [90, 85, 78]
print("Stored value marks[0]:", marks[0])
#include <stdio.h>
int main() {
    int marks[] = {90, 85, 78};
    printf("Stored value marks[0]: %d\n", marks[0]);
    return 0;
}

Algorithms

An algorithm is a precise, finite sequence of steps that turns an input into a correct output, like a recipe for solving a problem. The same problem can usually be solved by several different algorithms, and comparing their speed and memory use is what algorithm analysis is all about.

Example: Algorithms

#include <iostream>
using namespace std;
int main() {
    int nums[] = {4, 9, 2, 7};
    int maxVal = nums[0];
    for (int i = 1; i < 4; i++)
        if (nums[i] > maxVal) maxVal = nums[i];
    cout << "Max: " << maxVal << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        int[] nums = {4, 9, 2, 7};
        int maxVal = nums[0];
        for (int i = 1; i < 4; i++)
            if (nums[i] > maxVal) maxVal = nums[i];
        System.out.println("Max: " + maxVal);
    }
}
nums = [4, 9, 2, 7]
max_val = nums[0]
for x in nums[1:]:
    if x > max_val:
        max_val = x
print("Max:", max_val)
#include <stdio.h>
int main() {
    int nums[] = {4, 9, 2, 7};
    int maxVal = nums[0];
    for (int i = 1; i < 4; i++)
        if (nums[i] > maxVal) maxVal = nums[i];
    printf("Max: %d\n", maxVal);
    return 0;
}

Why DSA Matters

Without DSA, programs tend to work fine on small inputs and then grind to a halt as data grows, because naive approaches often repeat work unnecessarily. Understanding DSA lets you spot that kind of inefficiency early and choose an approach that stays fast even at scale, which is also why it's central to technical interviews.

Example: Why DSA Matters

#include <iostream>
using namespace std;
int main() {
    int nums[] = {1, 3, 5, 3};
    bool hasDup = false;
    for (int i = 0; i < 4; i++)
        for (int j = i + 1; j < 4; j++)
            if (nums[i] == nums[j]) hasDup = true;
    cout << "Has duplicate (naive O(n^2)): " << (hasDup ? "yes" : "no") << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        int[] nums = {1, 3, 5, 3};
        boolean hasDup = false;
        for (int i = 0; i < 4; i++)
            for (int j = i + 1; j < 4; j++)
                if (nums[i] == nums[j]) hasDup = true;
        System.out.println("Has duplicate (naive O(n^2)): " + hasDup);
    }
}
nums = [1, 3, 5, 3]
has_dup = False
for i in range(4):
    for j in range(i + 1, 4):
        if nums[i] == nums[j]:
            has_dup = True
print("Has duplicate (naive O(n^2)):", has_dup)
#include <stdio.h>
int main() {
    int nums[] = {1, 3, 5, 3};
    int hasDup = 0;
    for (int i = 0; i < 4; i++)
        for (int j = i + 1; j < 4; j++)
            if (nums[i] == nums[j]) hasDup = 1;
    printf("Has duplicate (naive O(n^2)): %s\n", hasDup ? "yes" : "no");
    return 0;
}

Basic DSA Thinking

Before writing code, get in the habit of asking three questions: what shape is the data, what operation do I need to perform on it, and what result am I expected to produce? Answering these first naturally points you toward the right data structure and algorithm instead of guessing.

Example: Basic DSA Thinking

#include <iostream>
using namespace std;
int main() {
    // Shape: array of ints. Operation: sum. Result: total.
    int nums[] = {5, 10, 15};
    int total = 0;
    for (int i = 0; i < 3; i++) total += nums[i];
    cout << "Total: " << total << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        // Shape: array of ints. Operation: sum. Result: total.
        int[] nums = {5, 10, 15};
        int total = 0;
        for (int i = 0; i < 3; i++) total += nums[i];
        System.out.println("Total: " + total);
    }
}
# Shape: list of ints. Operation: sum. Result: total.
nums = [5, 10, 15]
total = sum(nums)
print("Total:", total)
#include <stdio.h>
int main() {
    /* Shape: array of ints. Operation: sum. Result: total. */
    int nums[] = {5, 10, 15};
    int total = 0;
    for (int i = 0; i < 3; i++) total += nums[i];
    printf("Total: %d\n", total);
    return 0;
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.