Ready-to-Run Examples

Launch any snippet directly in the playground with one click. Includes algorithms, data structures, and core programming concepts.

10 curated examples available

Algorithms

Bubble Sort

javascript

Compare adjacent elements and repeatedly bubble the largest element to the end.

Great for visualizing nested loops and swap-heavy sorting behavior.

function bubbleSort(arr) {
  const a = [...arr];
  for (let i = 0; i < a.length - 1; i++) {
    for (let j = 0; j < a.length - 1 - i; j++) {
      if (a[j] > a[j + 1]) {
        [a[j], a[j + 1]] = [a[j + 1], a[j]];
      }
    }
  }
  return a;
}

const input = [8, 3, 1, 6, 2, 7];
console.log('Sorted:', bubbleSort(input));

Quick Sort

javascript

Divide-and-conquer sorting using a pivot and recursive partitioning.

Useful for recursion + partition visualization in one run.

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

  const pivot = arr[arr.length - 1];
  const left = [];
  const right = [];

  for (let i = 0; i < arr.length - 1; i++) {
    if (arr[i] < pivot) left.push(arr[i]);
    else right.push(arr[i]);
  }

  return [...quickSort(left), pivot, ...quickSort(right)];
}

const input = [9, 4, 2, 8, 1, 6, 3];
console.log('Sorted:', quickSort(input));

Binary Search

javascript

Search in a sorted array by halving the search space each step.

Perfect for understanding loop invariants and midpoint updates.

function binarySearch(arr, target) {
  let left = 0;
  let 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;
}

const values = [2, 4, 7, 11, 15, 19, 22, 31];
console.log('Index:', binarySearch(values, 19));

Data Structures

Stack

javascript

LIFO structure with push and pop operations.

Visualize call-like behavior and last-in-first-out flow.

class Stack {
  constructor() {
    this.items = [];
  }
  push(value) {
    this.items.push(value);
  }
  pop() {
    return this.items.pop();
  }
}

const stack = new Stack();
stack.push('first');
stack.push('second');
stack.push('third');
console.log(stack.pop());
console.log(stack.pop());

Queue

javascript

FIFO structure with enqueue and dequeue operations.

Ideal for comparing with stack behavior during execution.

class Queue {
  constructor() {
    this.items = [];
  }
  enqueue(value) {
    this.items.push(value);
  }
  dequeue() {
    return this.items.shift();
  }
}

const queue = new Queue();
queue.enqueue('A');
queue.enqueue('B');
queue.enqueue('C');
console.log(queue.dequeue());
console.log(queue.dequeue());

Linked List

javascript

Nodes connected by pointers, traversed sequentially.

Helpful for memory and pointer-like reasoning.

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

const head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);

let current = head;
while (current) {
  console.log(current.value);
  current = current.next;
}

Binary Tree

javascript

Hierarchical structure with left and right child nodes.

Excellent for recursion and traversal order intuition.

class TreeNode {
  constructor(value, left = null, right = null) {
    this.value = value;
    this.left = left;
    this.right = right;
  }
}

const root = new TreeNode(
  10,
  new TreeNode(5, new TreeNode(2), new TreeNode(7)),
  new TreeNode(14, null, new TreeNode(20))
);

function inOrder(node) {
  if (!node) return;
  inOrder(node.left);
  console.log(node.value);
  inOrder(node.right);
}

inOrder(root);

Programming Concepts

Recursion

javascript

A function calling itself with smaller inputs.

Watch call stack growth and unwind behavior clearly.

function factorial(n) {
  if (n <= 1) return 1;
  return n * factorial(n - 1);
}

console.log('factorial(5) =', factorial(5));

Event Loop

javascript

Microtasks and macrotasks scheduling in JavaScript runtime.

Essential for async debugging and interview prep.

console.log('Start');

setTimeout(() => {
  console.log('Timeout callback');
}, 0);

Promise.resolve()
  .then(() => {
    console.log('Promise microtask');
  })
  .then(() => {
    console.log('Chained microtask');
  });

console.log('End');

Memory Allocation

javascript

Visualize stack frames vs heap objects during function calls.

Useful for understanding references and garbage collection.

function createUser(name) {
  const user = {
    name,
    profile: {
      visits: 0,
      preferences: ['dark-mode']
    }
  };
  return user;
}

const alice = createUser('Alice');
alice.profile.visits += 1;
console.log(alice);