├── README.md ├── problem-1.js ├── problem-11.js ├── problem-3.js ├── problem-4.js ├── problem-6.js ├── problem-7.js ├── problem-9.js └── test.js /README.md: -------------------------------------------------------------------------------- 1 | # Problem Set 2 | 3 | This problem set contains a variety of fundamental algorithmic challenges that will help you sharpen your problem-solving skills. Each problem involves common data structure operations, numerical computations, or string manipulations. Below is a list of 12 problems, each accompanied by a short description and an example to help you understand the task. 4 | 5 | --- 6 | 7 | 1. **Rotate an Array** 8 | Write a function that rotates an array k times. Each rotation moves the last element of the array to the front. 9 | Example: `rotateArray([1, 2, 3, 4, 5], 2)` → `[4, 5, 1, 2, 3]` 10 | 11 | 2. **Find the Second Largest Number in an Array** 12 | Write a function that takes an array and returns the second largest number in the array. 13 | Example: `findSecondLargest([1, 5, 2, 3, 4])` → `4` 14 | 15 | 3. **Factorial of a Number** 16 | Write a function to calculate the factorial of a number. The factorial of n is the product of all positive integers less than or equal to n. 17 | Example: `factorial(5)` → `120` 18 | 19 | 4. **FizzBuzz Problem** 20 | Write a function that prints numbers from 1 to n. For multiples of 3, print "Fizz" instead of the number, for multiples of 5, print "Buzz", and for multiples of both, print "FizzBuzz". 21 | Example: `fizzBuzz(15)` should print: 22 | ``` 23 | 1 24 | 2 25 | Fizz 26 | 4 27 | Buzz 28 | Fizz 29 | 7 30 | 8 31 | Fizz 32 | Buzz 33 | 11 34 | Fizz 35 | 13 36 | 14 37 | FizzBuzz 38 | ``` 39 | 40 | 5. **Sum All Numbers in a Range** 41 | Write a function that takes an array of two numbers and returns the sum of all numbers between them (inclusive). 42 | Example: `sumAll([1, 4])` → `10` (1 + 2 + 3 + 4) 43 | 44 | 6. **Find Missing Number in an Array** 45 | Write a function that finds the missing number in an array containing numbers from 1 to n. 46 | Example: `findMissingNumber([1, 2, 4, 5, 6], 6)` → `3` 47 | 48 | 7. **Check for Anagram** 49 | Write a function to check if two given strings are anagrams of each other. An anagram is a word formed by rearranging the letters of another word. 50 | Example: `isAnagram("listen", "silent")` → `true` 51 | 52 | 8. **Check if a Number is Prime** 53 | Write a function that checks if a given number is a prime number. A prime number is a number greater than 1 that has no divisors other than 1 and itself. 54 | Example: `isPrime(7)` → `true` 55 | 56 | 9. **Find Intersection of Two Arrays** 57 | Write a function that takes two arrays and returns a new array with the common elements (intersection) between the two arrays. 58 | Example: `findIntersection([1, 2, 3], [2, 3, 4])` → `[2, 3]` 59 | 60 | 10. **Count Occurrences of Each Character in a String** 61 | Write a function that takes a string as input and returns an object where the keys are the characters from the string and the values are the number of times each character appears in the string. The function should be case-sensitive. 62 | Example: `countCharacters("hello world")` → `{ h: 1, e: 1, l: 3, o: 2, ' ': 1, w: 1, r: 1, d: 1 }` 63 | 64 | 11. **Merge Two Sorted Arrays** 65 | Write a function that merges two sorted arrays into one sorted array. 66 | Example: `mergeSortedArrays([1, 3, 5], [2, 4, 6])` → `[1, 2, 3, 4, 5, 6]` 67 | 68 | -------------------------------------------------------------------------------- /problem-1.js: -------------------------------------------------------------------------------- 1 | // Rotate an Array 2 | // Write a function that rotates an array k times. Each rotation moves the last element of the array to the front. 3 | // Example: rotateArray([1, 2, 3, 4, 5], 2) → [4, 5, 1, 2, 3] 4 | 5 | function rotateArray(arr, k) { 6 | const n = arr.length; 7 | const result = []; 8 | 9 | for (let i = 0; i < n; i++) { 10 | result[i] = arr[(n - k + i) % n] 11 | } 12 | console.log(result); 13 | } 14 | rotateArray([1, 2, 3, 4, 5], 3) 15 | 16 | 17 | /** 18 | * arr = [1, 2, 3, 4, 5] 19 | * n = 5 20 | * k = 2 21 | * 22 | * result = []; 23 | * result[0] = 4 = arr[3] // index = (n - k + i) % n = (5 - 2 + 0) % 5 = 3 % 5 = 3 24 | * result[1] = 5 = arr[4] // index = (n - k + i) % n = 5 - 2 + 1 = 4 % 5 = 4 25 | * result[2] = 1 = arr[0] // index = (n - k + i) % n = 5 - 2 + 2 = 5 % 5 = 0 26 | * result[3] = 2 = arr[1] // index = 6 % 5 = 1 27 | * result[4] = 3 = arr[2] // index = 7 % 5 = 2 28 | */ 29 | -------------------------------------------------------------------------------- /problem-11.js: -------------------------------------------------------------------------------- 1 | // Merge Two Sorted Arrays 2 | // Write a function that merges two sorted arrays into one sorted array. 3 | // Example: mergeSortedArrays([1, 3, 5], [2, 4, 6]) → [1, 2, 3, 4, 5, 6] 4 | 5 | function mergeSortedArrays(arr1, arr2) { 6 | let i = 0, j = 0; 7 | const result = []; 8 | 9 | while (i < arr1.length && j < arr2.length) { 10 | if (arr1[i] < arr2[j]) { 11 | result.push(arr1[i]); 12 | i++; 13 | } 14 | else { 15 | result.push(arr2[j]) 16 | j++; 17 | } 18 | } 19 | 20 | 21 | while (j < arr2.length) { 22 | result.push(arr2[j]); 23 | j++; 24 | } 25 | 26 | while (i < arr1.length) { 27 | result.push(arr1[i]); 28 | i++; 29 | } 30 | 31 | console.log(result) 32 | } 33 | 34 | mergeSortedArrays([1, 3, 5, 8, 9, 10], [2, 4, 6, 7]) -------------------------------------------------------------------------------- /problem-3.js: -------------------------------------------------------------------------------- 1 | // Factorial of a Number 2 | // Write a function to calculate the factorial of a number. The factorial of n is the product of all positive integers less than or equal to n. 3 | // Example: factorial(5) → 120 4 | 5 | /** 6 | * num = 3 7 | * fact(3) = 1 * 2 * 3 = 6 8 | * fact(4) = 1 * 2 * 3 * 4 = 24 9 | * fact(5) = 1 * 2 * 3 * 4 * 5 = 120 10 | */ 11 | 12 | function factorial(num) { 13 | let result = 1; 14 | for (let i = 2; i <= num; i++) { 15 | result = result * i; 16 | } 17 | 18 | console.log(result); 19 | } 20 | factorial(4); 21 | -------------------------------------------------------------------------------- /problem-4.js: -------------------------------------------------------------------------------- 1 | // FizzBuzz Problem 2 | // Write a function that prints numbers from 1 to n. For multiples of 3, print "Fizz" instead of the number, for multiples of 5, print "Buzz", and for multiples of both, print "FizzBuzz". 3 | // Example: fizzBuzz(15) should print: 4 | 5 | function fizzBuzz(n) { 6 | for (let i = 1; i <= n; i++) { 7 | if ((i % 3 === 0) && (i % 5 === 0)) { 8 | console.log("FizzBuzz") 9 | } 10 | else if (i % 3 === 0) { 11 | console.log("Fizz") 12 | } 13 | else if (i % 5 === 0) { 14 | console.log("Buzz") 15 | } 16 | 17 | else { 18 | console.log(i); 19 | } 20 | } 21 | } 22 | 23 | fizzBuzz(15); 24 | 25 | /** 26 | * 1 27 | * 2 28 | * Fizz 29 | * 4 30 | * Buzz 31 | * Fizz 32 | * 7 33 | * 8 34 | * Fizz 35 | * Buzz 36 | * 11 37 | * Fizz 38 | * 13 39 | * 14 40 | * FizzBuzz 41 | */ -------------------------------------------------------------------------------- /problem-6.js: -------------------------------------------------------------------------------- 1 | // Find Missing Number in an Array 2 | // Write a function that finds the missing number in an array containing numbers from 1 to n. 3 | // Example: findMissingNumber([1, 2, 4, 5, 6], 6) → 3 4 | 5 | function findMissingNumber(arr, n) { 6 | let sum = 0; 7 | for (let num of arr) { 8 | sum = sum + num; 9 | } 10 | 11 | const totalSum = n * (n + 1) / 2; 12 | const result = totalSum - sum; 13 | console.log(result); 14 | } 15 | 16 | findMissingNumber([1, 3, 4, 5, 6], 6) 17 | 18 | /** 19 | * n = 5 20 | * arr = [1, 5, 3, 4] = 13 21 | * actual arr= [1, 2, 3, 4, 5] = 15 22 | * r = 15- 13 = 2 23 | * 24 | * Sn = n(n+1)/2 = 5*(5+1) / 2 = 30 /2 = 15 25 | */ -------------------------------------------------------------------------------- /problem-7.js: -------------------------------------------------------------------------------- 1 | // Check for Anagram 2 | // Write a function to check if two given strings are anagrams of each other. An anagram is a word formed by rearranging the letters of another word. 3 | // Example: isAnagram("listen", "silent") → true 4 | 5 | /** 6 | * str1 = listen = eilnst 7 | * str2 = silent = eilnst 8 | */ -------------------------------------------------------------------------------- /problem-9.js: -------------------------------------------------------------------------------- 1 | // Find Intersection of Two Arrays 2 | // Write a function that takes two arrays and returns a new array with the common elements (intersection) between the two arrays. 3 | // Example: findIntersection([1, 2, 3], [2, 3, 4]) → [2, 3] 4 | 5 | function findIntersection(arr1, arr2) { 6 | const result = arr1.filter(num => arr2.includes(num)); 7 | console.log(result); 8 | } 9 | findIntersection([1, 2, 3], [6, 3, 4, 5]) 10 | /** 11 | * arr1 = [1, 2, 3] 12 | * arr2 = [2, 3, 4, 5] 13 | * r = [2, 3] 14 | * 15 | * 1 x 16 | * 2 17 | * 3 18 | */ -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | console.log("Next Level Web Development!") --------------------------------------------------------------------------------