JavaScript DSA Questions - Beginner to Advanced

February 10, 2025
Deepak Maurya
4 min read

A collection of JavaScript DSA questions with solutions and explanations from beginner to advanced.

Beginner Level

1. Reverse a String

Using Built-in Methods

function reverseString(str) {
  return str.split('').reverse().join('');
}

console.log(reverseString('hello')); // Output: "olleh"

Using Core Logic

function reverseStringManual(str) {
  let reversed = '';
  for (let i = str.length - 1; i >= 0; i--) {
    reversed += str[i];
  }
  return reversed;
}

console.log(reverseStringManual('hello')); // Output: "olleh"

2. Check for Palindrome

Using Built-in Methods

function isPalindrome(str) {
  return str === str.split('').reverse().join('');
}

console.log(isPalindrome('racecar')); // Output: true
console.log(isPalindrome('hello')); // Output: false

Using Core Logic

function isPalindromeManual(str) {
  let len = str.length;
  for (let i = 0; i < len / 2; i++) {
    if (str[i] !== str[len - 1 - i]) {
      return false;
    }
  }
  return true;
}

console.log(isPalindromeManual('racecar')); // Output: true
console.log(isPalindromeManual('hello')); // Output: false

3. Check if a Number is Prime

Problem: Write a function to check whether a number is prime.

function isPrime(n) {
  if (n < 2) return false;
  for (let i = 2; i <= Math.sqrt(n); i++) {
    if (n % i === 0) return false;
  }
  return true;
}
console.log(isPrime(7)); // Output: true
console.log(isPrime(10)); // Output: false

Explanation: A prime number has no divisors other than 1 and itself.


Intermediate Level

4. Fibonacci Series (Recursion)

Problem: Print the nth Fibonacci number.

function fibonacci(n) {
  if (n <= 1) return n;
  return fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(6)); // Output: 8

Explanation: Uses recursion to compute Fibonacci numbers.


5. Find the Largest Number in an Array

Using Built-in Methods

function findMax(arr) {
  return Math.max(...arr);
}

console.log(findMax([10, 20, 30, 5])); // Output: 30

Using Core Logic

function findMaxManual(arr) {
  let max = arr[0];
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] > max) {
      max = arr[i];
    }
  }
  return max;
}

console.log(findMaxManual([10, 20, 30, 5])); // Output: 30

6. Remove Duplicates from an Array

Using Built-in Methods

function removeDuplicates(arr) {
  return [...new Set(arr)];
}

Using Core Logic

function removeDuplicatesManual(arr) {
  let unique = [];
  for (let num of arr) {
    if (!unique.includes(num)) {
      unique.push(num);
    }
  }
  return unique;
}

7. Binary Search

Problem: Implement binary search on a sorted array.

function binarySearch(arr, target) {
  let left = 0,
    right = arr.length - 1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) return mid;
    else if (arr[mid] < target) left = mid + 1;
    else right = mid - 1;
  }
  return -1;
}
console.log(binarySearch([1, 2, 3, 4, 5], 3)); // Output: 2

Explanation: Uses a divide-and-conquer approach to efficiently search a sorted array.


Advanced Level

8. Longest Substring Without Repeating Characters

Problem: Find the length of the longest substring without repeating characters.

function longestUniqueSubstring(s) {
  let map = new Map(),
    maxLength = 0,
    left = 0;
  for (let right = 0; right < s.length; right++) {
    if (map.has(s[right])) {
      left = Math.max(map.get(s[right]) + 1, left);
    }
    map.set(s[right], right);
    maxLength = Math.max(maxLength, right - left + 1);
  }
  return maxLength;
}
console.log(longestUniqueSubstring('abcabcbb')); // Output: 3

Explanation: Uses a sliding window and a hashmap for efficient lookup.


9. Knapsack Problem (Dynamic Programming)

Problem: Solve 0/1 Knapsack problem.

function knapsack(weights, values, capacity) {
  let dp = Array(weights.length + 1)
    .fill()
    .map(() => Array(capacity + 1).fill(0));
  for (let i = 1; i <= weights.length; i++) {
    for (let w = 1; w <= capacity; w++) {
      if (weights[i - 1] <= w) {
        dp[i][w] = Math.max(
          values[i - 1] + dp[i - 1][w - weights[i - 1]],
          dp[i - 1][w]
        );
      } else {
        dp[i][w] = dp[i - 1][w];
      }
    }
  }
  return dp[weights.length][capacity];
}
console.log(knapsack([2, 3, 4, 5], [3, 4, 5, 6], 5)); // Output: 7

Explanation: Uses dynamic programming to maximize the value that fits within the given weight capacity.