Getting Started
DSA Introduction
Understand why data structures and algorithms matter for writing efficient, scalable code.
What is DSA?
Data Structures are ways to organize and store data. Algorithms are step-by-step procedures for solving problems.
Together, they are the foundation of computer science and the key to writing efficient software.
Why Learn DSA?
- Technical interviews: Google, Meta, Amazon, etc. test DSA heavily
- Performance: Know when your code is O(n) vs O(n²) — it matters at scale
- Problem solving: Think more clearly about complex problems
- Better developer: Understand how standard libraries work internally
Big O Notation
Big O describes how an algorithm's performance scales with input size:
| Notation | Name | Example |
|---|---|---|
| O(1) | Constant | Array access |
| O(log n) | Logarithmic | Binary search |
| O(n) | Linear | Linear search |
| O(n log n) | Log-linear | Merge sort |
| O(n²) | Quadratic | Bubble sort |
| O(2ⁿ) | Exponential | Recursive Fibonacci |
Time vs Space Complexity
- Time complexity: How long an algorithm takes
- Space complexity: How much memory it uses
Example
javascript
// Big O examples in JavaScript
// O(1) - Constant time
function getFirst(arr) {
return arr[0]; // always one operation regardless of array size
}
// O(n) - Linear time
function findMax(arr) {
let max = arr[0];
for (let i = 1; i < arr.length; i++) { // n iterations
if (arr[i] > max) max = arr[i];
}
return max;
}
// O(n²) - Quadratic time
function hasDuplicate(arr) {
for (let i = 0; i < arr.length; i++) { // n
for (let j = i + 1; j < arr.length; j++) { // n
if (arr[i] === arr[j]) return true;
}
}
return false;
}
// O(n) with hash map - much better!
function hasDuplicateFast(arr) {
const seen = new Set();
for (const item of arr) {
if (seen.has(item)) return true;
seen.add(item);
}
return false;
}
// O(log n) - Binary search
function binarySearch(arr, target) {
let left = 0, right = arr.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}Try it yourself — JAVASCRIPT