50% off all plans with SPRING50

Easy Practice Problems

14 easy problems out of 40 total. Perfect for warming up and learning fundamentals.

Palindrome

easy

Write a function `isPalindrome` that determines if a given string is a palindrome. A string is considered a palindrome if it reads the same forward and backward, ignoring cases, spaces, and punctuation. ## Function Signature ```function isPalindrome(str);``` ### Example 1: > **Input:** `s = "racecar"`\ > **Output:** `true`\ > **Explanation:** The string "racecar" reads the same forward and backward. ### Example 2: > **Input:** `s = "hello"`\ > **Output:** `false`\ > **Explanation:** The string "hello" does not read the same forward and backward. ### Example 3: > **Input:** `s = "kajak"`\ > **Output:** `true`\ > **Explanation:** The string "kajak" reads the same forward and backward, proving it is a palindrome.

String

Two Sum

easy

Write a function `twoSum` that takes two integers `a` and `b` and returns their sum. ## Function Signature ```function twoSum(a, b);``` ### Example 1: > **Input:** `a = 3, b = 5`\ > **Output:** `8`\ > **Explanation:** The sum of `3` and `5` is `8`. ### Example 2: > **Input:** `a = -2, b = -4`\ > **Output:** `-6`\ > **Explanation:** The sum of `-2` and `-4` is `-6`. ### Example 3: > **Input:** `a = 100, b = 200`\ > **Output:** `300`\ > **Explanation:** The sum of `100` and `200` is `300`.

Math

Sort Array

easy

Write a function `sortArray` that takes an array of integers and returns a new array with the elements sorted in ascending order. ## Function Signature ```function sortArray(nums);``` ### Example 1: > **Input:** `nums = [34, 7, 23, 32, 5, 62]`\ > **Output:** `[5, 7, 23, 32, 34, 62]`\ > **Explanation:** After sorting the numbers from the smallest to the largest, the output is `[5, 7, 23, 32, 34, 62]`. ### Example 2: > **Input:** `nums = [-1, -3, -2, 0, 2, 1]`\ > **Output:** `[-3, -2, -1, 0, 1, 2]`\ > **Explanation:** Negative numbers are sorted first followed by zero and positive numbers, resulting in `[-3, -2, -1, 0, 1, 2]`. ### Example 3: > **Input:** `nums = [10]`\ > **Output:** `[10]`\ > **Explanation:** Since there is only one element in the array, the sorted array remains `[10]`.

Array

Find Max

easy

Write a function `findMax` that takes an array of integers and returns the maximum value. ## Function Signature ```function findMax(nums);``` ### Example 1: > **Input:** `nums = [1, 5, 3, 9, 2]`\ > **Output:** `9`\ > **Explanation:** The maximum value in the array is `9`. ### Example 2: > **Input:** `nums = [-1, -5, -3, -9, -2]`\ > **Output:** `-1`\ > **Explanation:** The maximum value in the array is `-1`. ### Example 3: > **Input:** `nums = [10]`\ > **Output:** `10`\ > **Explanation:** The maximum value in the array is `10`.

Array

Reverse String

easy

Write a function `reverseString` that takes a string and returns it reversed. ## Function Signature ```function reverseString(str);``` ### Example 1: > **Input:** `str = "hello"`\ > **Output:** `"olleh"`\ > **Explanation:** The string "hello" reversed is "olleh". ### Example 2: > **Input:** `str = "world"`\ > **Output:** `"dlrow"`\ > **Explanation:** The string "world" reversed is "dlrow". ### Example 3: > **Input:** `str = "a"`\ > **Output:** `"a"`\ > **Explanation:** The string "a" reversed is "a".

String

Sum Array

easy

Write a function `sumArray` that takes an array of integers and returns the sum of all elements. ## Function Signature ```function sumArray(nums);``` ### Example 1: > **Input:** `nums = [1, 2, 3, 4, 5]`\ > **Output:** `15`\ > **Explanation:** The sum of the numbers is `1 + 2 + 3 + 4 + 5 = 15`. ### Example 2: > **Input:** `nums = [-1, -2, -3, -4, -5]`\ > **Output:** `-15`\ > **Explanation:** The sum of the numbers is `-1 + -2 + -3 + -4 + -5 = -15`. ### Example 3: > **Input:** `nums = [0]`\ > **Output:** `0`\ > **Explanation:** The sum of the numbers is `0`.

Array

Multiply

easy

Write a function `multiply` that takes two integers `a` and `b` and returns their product. ## Function Signature ```function multiply(a, b);``` ### Example 1: > **Input:** `a = 3, b = 5`\ > **Output:** `15`\ > **Explanation:** The product of `3` and `5` is `15`. ### Example 2: > **Input:** `a = -2, b = -4`\ > **Output:** `8`\ > **Explanation:** The product of `-2` and `-4` is `8`. ### Example 3: > **Input:** `a = 0, b = 200`\ > **Output:** `0`\ > **Explanation:** The product of `0` and `200` is `0`.

Math

Count Vowels

easy

Write a function `countVowels` that takes a string and returns the number of vowels in it. ## Function Signature ```function countVowels(str);``` ### Example 1: > **Input:** `str = "hello"`\ > **Output:** `2`\ > **Explanation:** The string "hello" contains 2 vowels: "e" and "o". ### Example 2: > **Input:** `str = "world"`\ > **Output:** `1`\ > **Explanation:** The string "world" contains 1 vowel: "o". ### Example 3: > **Input:** `str = "bcdfgh"`\ > **Output:** `0`\ > **Explanation:** The string "bcdfgh" contains no vowels.

String

Is Even

easy

Write a function `isEven` that takes an integer and returns true if it is even, false otherwise. ## Function Signature ```function isEven(num);``` ### Example 1: > **Input:** `num = 4`\ > **Output:** `true`\ > **Explanation:** The number 4 is even. ### Example 2: > **Input:** `num = 7`\ > **Output:** `false`\ > **Explanation:** The number 7 is not even. ### Example 3: > **Input:** `num = 0`\ > **Output:** `true`\ > **Explanation:** The number 0 is even.

Math

Factorial

easy

Write a function `factorial` that takes a non-negative integer and returns its factorial. ## Function Signature ```function factorial(num);``` ### Example 1: > **Input:** `num = 5`\ > **Output:** `120`\ > **Explanation:** The factorial of 5 is `5! = 5 * 4 * 3 * 2 * 1 = 120`. ### Example 2: > **Input:** `num = 0`\ > **Output:** `1`\ > **Explanation:** The factorial of 0 is defined as `1`. ### Example 3: > **Input:** `num = 3`\ > **Output:** `6`\ > **Explanation:** The factorial of 3 is `3! = 3 * 2 * 1 = 6`.

Math

Find Min

easy

Write a function `findMin` that takes an array of integers and returns the minimum value. ## Function Signature ```function findMin(nums);``` ### Example 1: > **Input:** `nums = [1, 5, 3, 9, 2]`\ > **Output:** `1`\ > **Explanation:** The minimum value in the array is `1`. ### Example 2: > **Input:** `nums = [-1, -5, -3, -9, -2]`\ > **Output:** `-9`\ > **Explanation:** The minimum value in the array is `-9`. ### Example 3: > **Input:** `nums = [10]`\ > **Output:** `10`\ > **Explanation:** The minimum value in the array is `10`.

Array

Add Digits

easy

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ## Function Signature ```function addDigits(num);``` ### Example 1: > **Input:** `num = 38` \ > **Output:** `2` \ > **Explanation:** The process is 3+8 = 11, 1+1 = 2. Since 2 has only one digit, 2 is returned. ### Example 2: > **Input:** `num = 999` \ > **Output:** `9` \ > **Explanation:** The process is 9+9+9 = 27, 2+7 = 9. Since 9 has only one digit, 9 is returned.

Math

Calculate Fibonacci Number

easy

Calculate the n-th Fibonacci number. ## Function Signature ```function fibonacci(n);``` ### Example 1: > **Input:** `n = 5` \ > **Output:** `5` \ > **Explanation:** The 5th Fibonacci number is 5 (0, 1, 1, 2, 3, 5). ### Example 2: > **Input:** `n = 10` \ > **Output:** `55` \ > **Explanation:** The 10th Fibonacci number is 55.

Math

Maximum Product of Two Elements

easy

Find the maximum product of two distinct elements in an array of integers. ## Function Signature ```function maxProduct(nums);``` ### Example 1: > **Input:** `nums = [3,4,5,2]` \ > **Output:** `20` \ > **Explanation:** The maximum product (5*4 = 20) is obtained by multiplying the two largest elements. ### Example 2: > **Input:** `nums = [1,5,4,5]` \ > **Output:** `25` \ > **Explanation:** The maximum product (5*5 = 25) is obtained by multiplying the two largest elements.

Array

Ready to practice?

Solve all 40 problems in our built-in code editor with instant feedback.

Start 7-Day Free Trial