DSAIntroduction to DSA

Introduction to DSA

Every app you use daily — Google Search, Google Maps, your bank website, Spotify — runs on the same foundation: Data Structures and Algorithms. When you search for a route in Google Maps, a shortest-path algorithm finds it in milliseconds across millions of roads. When Spotify recommends a song, a graph algorithm traverses your listening history and the histories of millions of similar users. When your database finds a record among 500 million rows, a tree structure makes it instant.

DSA is not an academic curiosity. It is the engine underneath every piece of software that performs well at scale. This series teaches you to think the way computers think — and to design solutions that are both correct and efficient.

What are Data Structures?

A data structure is a way of organizing and storing data so that it can be accessed and modified efficiently. Think of it as a container with rules. Different containers suit different jobs:

Data Structure

Real-world analogy

Best for

Array

Numbered lockers in a row

Fast access by index

Linked List

A chain of paper clips

Fast insertions/deletions

Stack

A stack of plates

Undo/redo, call stack

Queue

A line at the bank

Scheduling, BFS

Hash Table

A dictionary index

Fast key-value lookup

Tree

A company org chart

Hierarchical data, search

Graph

A road map

Networks, relationships

Heap

A priority boarding queue

Finding min/max fast

What are Algorithms?

An algorithm is a precise, step-by-step set of instructions that solves a well-defined problem. Every algorithm has:

  • Input — one or more values given to the algorithm

  • Output — one or more results produced

  • Definiteness — each step is clear and unambiguous

  • Finiteness — the algorithm terminates after a finite number of steps

  • Effectiveness — each step is basic enough to be carried out

Note
The word "algorithm" comes from the name of the 9th-century Persian mathematician Muhammad ibn Musa al-Khwarizmi, whose work on arithmetic was translated into Latin and became the foundation of European mathematics.
How Computers Store Data

Before you can understand data structures, you need a mental model of computer memory. RAM (Random Access Memory) is a giant array of numbered slots, each holding one byte. When you declare a variable, the operating system assigns it one or more contiguous slots.

JS
// In memory, an array of integers is stored contiguously:
// Address:  1000  1004  1008  1012  1016
// Value:    [  5] [ 12] [  8] [  3] [ 20]

const arr = [5, 12, 8, 3, 20]

// Each integer uses 4 bytes (32-bit).
// arr[0] lives at address 1000
// arr[1] lives at address 1004
// arr[i] lives at address: 1000 + (i * 4)  <- constant-time access!

// Accessing any element: single arithmetic operation
console.log(arr[3])  // address = 1000 + 3*4 = 1012 -> value 3

Because array elements are stored contiguously (side by side), the CPU can jump to any element in a single step using simple arithmetic. This is why arrays have O(1) (constant time) random access — a property that underpins many efficient algorithms.

The Connection Between Data Structures and Algorithms

Data structures and algorithms are inseparable. The choice of data structure determines which algorithms are efficient, and the algorithm determines which data structure is appropriate. Consider finding whether a number exists in a collection of one million values:

JS
// Option 1: Unsorted array — linear search is your only option
function linearSearch(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) return i
  }
  return -1
}
// Time: O(n) — must check up to 1,000,000 elements

// Option 2: Hash Set — O(1) lookup, no search needed
const set = new Set([/* 1 million values */])
set.has(99)   // O(1) — essentially instant regardless of size

// Option 3: Sorted array — binary search halves the problem each step
function binarySearch(arr, target) {
  let lo = 0, hi = arr.length - 1
  while (lo <= hi) {
    const mid = Math.floor((lo + hi) / 2)
    if (arr[mid] === target) return mid
    else if (arr[mid] < target) lo = mid + 1
    else hi = mid - 1
  }
  return -1
}
// Time: O(log n) — 1 million elements needs only 20 comparisons!
Searching 1,000,000 elements:
  Linear search:  up to 1,000,000 comparisons
  Binary search:  at most 20 comparisons
  Hash set:       1 lookup (amortized)
Tip
The right data structure can turn an impossible problem into a trivial one. A program that takes hours with a naive approach can run in milliseconds with the right structure. This is the core promise of learning DSA.
Real-World Use Cases

Domain

Problem

Data Structure / Algorithm

Search Engines

Find all pages containing a keyword instantly

Inverted index (Hash Map), PageRank (Graph + BFS)

GPS / Navigation

Find shortest driving route

Dijkstra's algorithm on a weighted graph

Databases

Find a row among billions of records

B-Tree index (balanced search tree)

Social Networks

People you may know suggestions

Graph BFS up to degree N

Autocomplete

Suggest completions as you type

Trie (prefix tree)

Compilers

Check if braces/parentheses are balanced

Stack

Operating Systems

Schedule CPU tasks by priority

Priority Queue (Min-Heap)

Streaming

Find the median of a data stream

Two heaps (max + min)

How Google Search Works (Simplified)

When you type "best coffee shops NYC" into Google, here is a simplified view of what happens under the hood — every step is a DSA concept you will learn in this series:

  1. Tokenization — the query splits into tokens: ["best", "coffee", "shops", "NYC"]

  2. Inverted index lookup — for each token, a hash map returns millions of document IDs in microseconds

  3. Set intersection — sorted arrays are merged to find documents containing all terms

  4. Ranking — PageRank scores (computed offline via graph traversal) and signals sort the results

  5. Top-K extraction — a min-heap efficiently returns only the top 10 results without sorting all matches

How to Think Like a Computer Scientist

Professional engineers do not memorize algorithms — they develop pattern recognition. After encountering enough problems, you begin to see that "find shortest path" always points toward BFS or Dijkstra, that "detect a cycle" points toward Union-Find or DFS with coloring, that "optimal substructure" points toward Dynamic Programming.

This pattern recognition comes from three things: understanding why each data structure exists (what problem it solves), understanding how each algorithm works mechanically, and practicing enough problems until the patterns become instinct.

What You Will Learn in This Series

Phase

Topics

Foundations

Complexity analysis, Big-O notation, arrays, strings, recursion

Linear Structures

Linked lists, stacks, queues, hash tables

Non-linear Structures

Trees, binary search trees, heaps, graphs, tries

Core Algorithms

Sorting, searching, BFS/DFS, two pointers, sliding window

Advanced Algorithms

Dynamic programming, greedy, divide and conquer, backtracking

Advanced Structures

Segment trees, union-find, topological sort, shortest paths

Interview Prep

Problem patterns, FAANG-style questions, time management

Prerequisites

You need basic programming knowledge in any language — variables, loops, conditionals, and functions. All code examples in this series use JavaScript (with occasional Python for comparison), but every concept applies to any language. If you can write a loop and call a function, you are ready to begin.

Your First DSA Program

Let us start with something concrete — a program that demonstrates why algorithm choice matters, even on small inputs:

JS
// Compare how different algorithms scale as input grows
function compareGrowth(n) {
  console.log(`Input size n = ${n}:`)
  console.log(`  O(1)       : 1 operation`)
  console.log(`  O(log n)   : ${Math.ceil(Math.log2(n))} operations`)
  console.log(`  O(n)       : ${n} operations`)
  console.log(`  O(n log n) : ${Math.ceil(n * Math.log2(n))} operations`)
  console.log(`  O(n^2)     : ${n * n} operations`)
  console.log(`  O(2^n)     : ${n <= 30 ? Math.pow(2, n) : 'astronomical'} operations`)
  console.log('')
}

compareGrowth(10)
compareGrowth(100)
compareGrowth(1000)
Input size n = 10:
  O(1)       : 1 operation
  O(log n)   : 4 operations
  O(n)       : 10 operations
  O(n log n) : 34 operations
  O(n^2)     : 100 operations
  O(2^n)     : 1024 operations

Input size n = 100:
  O(1)       : 1 operation
  O(log n)   : 7 operations
  O(n)       : 100 operations
  O(n log n) : 665 operations
  O(n^2)     : 10,000 operations
  O(2^n)     : astronomical operations

Input size n = 1000:
  O(1)       : 1 operation
  O(log n)   : 10 operations
  O(n)       : 1,000 operations
  O(n log n) : 9,966 operations
  O(n^2)     : 1,000,000 operations
  O(2^n)     : astronomical operations
Warning
A common beginner mistake is micro-optimizing O(n) code when the real bottleneck is an O(n²) algorithm nearby. Always identify your worst complexity first — a 10x faster constant does nothing against an exponential growth rate.
Summary
  • A data structure organizes data; an algorithm is a procedure that solves a problem using that data

  • The choice of data structure fundamentally determines algorithm performance

  • Real-world systems — search engines, GPS, databases — rely on these concepts for scale

  • DSA teaches you to reason about efficiency, not just correctness

  • Pattern recognition from practice is the key skill — you learn it by doing, not just reading

Up next
The next page covers **why you should invest time in learning DSA** — career impact, problem-solving skills, and what top companies actually test in their interviews.