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.
-
Rotate an Array
Write a function that rotates an array k times. Each rotation moves the last element of the array to the front.
Example:rotateArray([1, 2, 3, 4, 5], 2)
→[4, 5, 1, 2, 3]
-
Find the Second Largest Number in an Array
Write a function that takes an array and returns the second largest number in the array.
Example:findSecondLargest([1, 5, 2, 3, 4])
→4
-
Factorial of a Number
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.
Example:factorial(5)
→120
-
FizzBuzz Problem
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".
Example:fizzBuzz(15)
should print:1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
-
Sum All Numbers in a Range
Write a function that takes an array of two numbers and returns the sum of all numbers between them (inclusive).
Example:sumAll([1, 4])
→10
(1 + 2 + 3 + 4) -
Find Missing Number in an Array
Write a function that finds the missing number in an array containing numbers from 1 to n.
Example:findMissingNumber([1, 2, 4, 5, 6], 6)
→3
-
Check for Anagram
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.
Example:isAnagram("listen", "silent")
→true
-
Check if a Number is Prime
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.
Example:isPrime(7)
→true
-
Find Intersection of Two Arrays
Write a function that takes two arrays and returns a new array with the common elements (intersection) between the two arrays.
Example:findIntersection([1, 2, 3], [2, 3, 4])
→[2, 3]
-
Count Occurrences of Each Character in a String
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.
Example:countCharacters("hello world")
→{ h: 1, e: 1, l: 3, o: 2, ' ': 1, w: 1, r: 1, d: 1 }
-
Merge Two Sorted Arrays
Write a function that merges two sorted arrays into one sorted array.
Example:mergeSortedArrays([1, 3, 5], [2, 4, 6])
→[1, 2, 3, 4, 5, 6]