Top 20 JavaScript related DSA question answers

 Here’s a list of 20 popular JavaScript-related Data Structures and Algorithms (DSA) questions commonly asked in competitive programming, coding interviews, or during job applications. Along with each question, I’ve provided the solution in JavaScript.


1. Reverse a String

Problem: Reverse a given string.


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

2. Check for Palindrome

Problem: Check if a string is a palindrome.


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

3. Fibonacci Sequence

Problem: Find the nth Fibonacci number.


function fibonacci(n) { let a = 0, b = 1; for (let i = 2; i <= n; i++) { [a, b] = [b, a + b]; } return n === 0 ? a : b; }

4. Find Factorial

Problem: Find the factorial of a number.


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

5. Sum of Natural Numbers

Problem: Find the sum of the first N natural numbers.


function sumOfNumbers(n) { return (n * (n + 1)) / 2; }

6. Binary Search

Problem: Perform binary search on a sorted array.


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; }

7. Merge Sort

Problem: Sort an array using merge sort.


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) { let result = [], 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.concat(left.slice(i), right.slice(j)); }

8. Quick Sort

Problem: Sort an array using quick sort.


function quickSort(arr) { if (arr.length <= 1) return arr; const pivot = arr[arr.length - 1]; const left = [], right = []; for (let i = 0; i < arr.length - 1; i++) { arr[i] < pivot ? left.push(arr[i]) : right.push(arr[i]); } return [...quickSort(left), pivot, ...quickSort(right)]; }

9. Merge Two Sorted Arrays

Problem: Merge two sorted arrays.


function mergeArrays(arr1, arr2) { let result = []; let i = 0, j = 0; while (i < arr1.length && j < arr2.length) { if (arr1[i] < arr2[j]) result.push(arr1[i++]); else result.push(arr2[j++]); } return result.concat(arr1.slice(i), arr2.slice(j)); }

10. Two Sum

Problem: Find two numbers in an array that add up to a given target.


function twoSum(nums, target) { let 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); } return []; }

11. Reverse Linked List

Problem: Reverse a linked list.


function reverseLinkedList(head) { let prev = null, current = head; while (current !== null) { const nextNode = current.next; current.next = prev; prev = current; current = nextNode; } return prev; }

12. Find Middle of Linked List

Problem: Find the middle element of a linked list.


function findMiddle(head) { let slow = head, fast = head; while (fast !== null && fast.next !== null) { slow = slow.next; fast = fast.next.next; } return slow; }

13. Detect Cycle in Linked List

Problem: Detect if there is a cycle in a linked list.


function hasCycle(head) { let slow = head, fast = head; while (fast !== null && fast.next !== null) { slow = slow.next; fast = fast.next.next; if (slow === fast) return true; } return false; }

14. Find Maximum Subarray (Kadane's Algorithm)

Problem: Find the maximum sum of a contiguous subarray.


function maxSubArray(nums) { let maxSoFar = nums[0], maxEndingHere = nums[0]; for (let i = 1; i < nums.length; i++) { maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]); maxSoFar = Math.max(maxSoFar, maxEndingHere); } return maxSoFar; }

15. Container With Most Water

Problem: Find the container with the most water.


function maxArea(height) { let left = 0, right = height.length - 1; let maxArea = 0; while (left < right) { const area = Math.min(height[left], height[right]) * (right - left); maxArea = Math.max(maxArea, area); if (height[left] < height[right]) left++; else right--; } return maxArea; }

16. Find All Anagrams in a String

Problem: Find all the start indices of p's anagrams in s.


function findAnagrams(s, p) { let result = []; if (s.length < p.length) return result; const pCount = Array(26).fill(0); const sCount = Array(26).fill(0); for (let i = 0; i < p.length; i++) { pCount[p.charCodeAt(i) - 97]++; sCount[s.charCodeAt(i) - 97]++; } if (arraysEqual(pCount, sCount)) result.push(0); for (let i = p.length; i < s.length; i++) { sCount[s.charCodeAt(i) - 97]++; sCount[s.charCodeAt(i - p.length) - 97]--; if (arraysEqual(pCount, sCount)) { result.push(i - p.length + 1); } } return result; } function arraysEqual(a, b) { for (let i = 0; i < a.length; i++) { if (a[i] !== b[i]) return false; } return true; }

17. Valid Parentheses

Problem: Check if the parentheses in a string are valid.


function isValid(s) { let stack = []; for (let char of s) { if (char === '(' || char === '[' || char === '{') { stack.push(char); } else { if (stack.length === 0) return false; let top = stack.pop(); if (char === ')' && top !== '(') return false; if (char === ']' && top !== '[') return false; if (char === '}' && top !== '{') return false; } } return stack.length === 0; }

18. Find the Intersection of Two Arrays

Problem: Find the intersection of two arrays.


function intersection(nums1, nums2) { let set1 = new Set(nums1); let result = []; for (let num of nums2) { if (set1.has(num)) { result.push(num); set1.delete(num); } } return result; }

19. Rotate Image (Matrix)

Problem: Rotate an n x n 2D matrix 90 degrees.


function rotate(matrix) { let n = matrix.length; for (let i = 0; i < Math.floor(n / 2); i++) { for (let j = i; j < n - i - 1; j++) { let temp = matrix[i][j]; matrix[i][j] = matrix[n - j - 1][i]; matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1]; matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1]; matrix[j][n - i - 1] = temp; } } }

20. Longest Substring Without Repeating Characters

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


function lengthOfLongestSubstring(s) { let map = new Map(); let maxLength = 0; let start = 0; for (let end = 0; end < s.length; end++) { if (map.has(s[end])) { start = Math.max(map.get(s[end]) + 1, start); } map.set(s[end], end); maxLength = Math.max(maxLength, end - start + 1); } return maxLength; }

Comments

Popular posts from this blog

Top 200 telagu words in hindi daily life?

List of AI certificates which are valid in IT industry

Online platforms to code in react, Angular