Skip to content
All essays
ArchitectureFebruary 16, 202511 min

Data Structures and Algorithms: Practical Guide

Master essential data structures and algorithms. Practical guide for interviews and real-world coding.

Ü
Ümit Uz
Mobile & Full Stack Developer

Arrays

javascript
// Two-pointer technique
function reverseArray(arr) {
  let left = 0;
  let right = arr.length - 1;

  while (left < right) {
    [arr[left], arr[right]] = [arr[right], arr[left]];
    left++;
    right--;
  }
  return arr;
}

Linked Lists

javascript
class ListNode {
  constructor(val) {
    this.val = val;
    this.next = null;
  }
}

function reverseLinkedList(head) {
  let prev = null;
  let current = head;

  while (current) {
    const next = current.next;
    current.next = prev;
    prev = current;
    current = next;
  }
  return prev;
}

Stacks and Queues

javascript
// Stack implementation
class Stack {
  constructor() {
    this.items = [];
  }

  push(item) { this.items.push(item); }
  pop() { return this.items.pop(); }
  peek() { return this.items[this.items.length - 1]; }
  isEmpty() { return this.items.length === 0; }
}

// Queue implementation
class Queue {
  constructor() {
    this.items = [];
  }

  enqueue(item) { this.items.push(item); }
  dequeue() { return this.items.shift(); }
  peek() { return this.items[0]; }
  isEmpty() { return this.items.length === 0; }
}

Hash Maps

javascript
// Two Sum problem
function twoSum(nums, target) {
  const map = new Map();

  for (let i = 0; i < nums.length; i++) {
    const complement = target - nums[i];
    if (map.has(complement)) {
      return [map.get(complement), i];
    }
    map.set(nums[i], i);
  }
}

Trees

Binary Search Tree

javascript
class TreeNode {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function inorderTraversal(root) {
  const result = [];
  function traverse(node) {
    if (!node) return;
    traverse(node.left);
    result.push(node.val);
    traverse(node.right);
  }
  traverse(root);
  return result;
}

function searchBST(root, val) {
  if (!root) return null;
  if (root.val === val) return root;
  if (val < root.val) return searchBST(root.left, val);
  return searchBST(root.right, val);
}

Level Order Traversal

javascript
function levelOrder(root) {
  if (!root) return [];
  const result = [];
  const queue = [root];

  while (queue.length > 0) {
    const level = [];
    const size = queue.length;

    for (let i = 0; i < size; i++) {
      const node = queue.shift();
      level.push(node.val);
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
    }
    result.push(level);
  }
  return result;
}

Graphs

javascript
function bfs(graph, start) {
  const visited = new Set();
  const queue = [start];
  const result = [];

  while (queue.length > 0) {
    const node = queue.shift();
    if (!visited.has(node)) {
      visited.add(node);
      result.push(node);
      queue.push(...graph[node]);
    }
  }
  return result;
}
javascript
function dfs(graph, start, visited = new Set()) {
  if (visited.has(start)) return [];
  visited.add(start);
  const result = [start];

  for (const neighbor of graph[start]) {
    result.push(...dfs(graph, neighbor, visited));
  }
  return result;
}

Sorting Algorithms

Merge Sort

javascript
function mergeSort(arr) {
  if (arr.length <= 1) return arr;

  const mid = Math.floor(arr.length / 2);
  const left = mergeSort(arr.slice(0, mid));
  const right = mergeSort(arr.slice(mid));

  return merge(left, right);
}

function merge(left, right) {
  const result = [];
  let i = 0, j = 0;

  while (i < left.length && j < right.length) {
    if (left[i] < right[j]) {
      result.push(left[i++]);
    } else {
      result.push(right[j++]);
    }
  }
  return [...result, ...left.slice(i), ...right.slice(j)];
}

Time Complexity

| Algorithm | Best | Average | Worst | |-----------|------|---------|-------| | Quick Sort | O(n log n) | O(n log n) | O(n²) | | Merge Sort | O(n log n) | O(n log n) | O(n log n) | | Binary Search | O(1) | O(log n) | O(log n) | | BFS | O(V + E) | O(V + E) | O(V + E) |

Practice Strategy

  1. 1Pattern recognition: Learn common patterns
  2. 2Spaced repetition: Revisit problems periodically
  3. 3Mock interviews: Practice under time pressure
  4. 4Read discussions: Learn optimal solutions

Practice makes perfect!

Related essays

Next essay
Introduction to Machine Learning: A Developer Guide