-
FizzBuzz - Write a program that prints the numbers from 1 to n, replacing multiples of 3 with the string "Fizz" and multiples of 5 with the string "Buzz".
-
Palindrome - Write a program to check if a given string is a palindrome.
-
Reverse a string - Write a program to reverse a given string.
-
Two Sum - Write a function that takes in an array and a target number and returns the indices of two numbers that add up to the target.
-
Longest Common Prefix - Write a function that takes in an array of strings and returns the longest common prefix.
-
Merge two sorted arrays - Write a function that takes in two sorted arrays and returns a new array that is also sorted.
-
Maximum Subarray - Write a function that takes in an array of numbers and returns the sum of the largest contiguous subarray.
Example:
// Input: [-2, 1, -3, 4, -1, 2, 1, -5, 4]
// Output: [4, -1, 2, 1]
// Input: [1, 2, 3, 4, 5]
// Output: [1, 2, 3, 4, 5]
// Input: [-1, -2, -3, -4, -5]
// Output: [-1]
- Pascal's Triangle - Write a function that generates Pascal's Triangle up to a given number of rows.
- Factorial - Write a function to compute the factorial of a given number.
- Fibonacci - Write a function to compute the nth Fibonacci number.
- Write a function fibonacci(n) that calculates the nth number in the Fibonacci sequence.
// Input: fibonacci(6)
// Output: 8
// Explanation: The 6th number in the Fibonacci sequence is 8.
- Write a function palindrome(str) that checks if a string is a palindrome (a word or phrase that reads the same forwards and backwards).
// Input: palindrome("racecar")
// Output: true
// Explanation: "racecar" is a palindrome, so the function returns true.
// Input: palindrome("hello")
// Output: false
// Explanation: "hello" is not a palindrome, so the function returns false.
- Write a function flattenArray(arr) that takes a nested array and flattens it into a single-dimensional array.
// Input: flattenArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
// Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
// Explanation: The nested array is flattened into a single-dimensional array.
- Write a function anagrams(str1, str2) that checks if two strings are anagrams (have the same letters in different order).
// Input: anagrams("listen", "silent")
// Output: true
// Explanation: "listen" and "silent" have the same letters in different order, so they are anagrams and the function returns true.
// Input: anagrams("hello", "world")
// Output: false
// Explanation: "hello" and "world" do not have the same letters in different order, so they are not anagrams and the function returns false.
- Write a function twoSum(arr, target) that finds the two numbers in an array that add up to the target sum.
// Input: twoSum([2, 7, 11, 15], 9)
// Output: [0, 1]
// Explanation: The two numbers in the array that add up to 9 are 2 and 7, which have indices 0 and 1, respectively.
// Input: twoSum([3, 2, 4], 6)
// Output: [1, 2]
// Explanation: The two numbers in the array that add up to 6 are 2 and 4, which have indices 1 and 2, respectively.
- Write a function maxSubArray(arr) that finds the contiguous subarray with the largest sum in a given array.
// Input: maxSubArray([-2, 1, -3, 4, -1, 2, 1, -5, 4])
// Output: 6
// Explanation: The largest sum is achieved by the contiguous subarray [4, -1, 2, 1], which has a sum of 6.
- Write a function reverseInt(num) that reverses an integer (e.g. 123 -> 321).
// Input: reverseInt(123)
// Output: 321
// Explanation: The reverse of 123 is 321.
// Input: reverseInt(-456)
// Output: -654
// Explanation: The reverse of -456 is -654.
- Write a function isValidBracketSequence(str) that checks if a string contains a valid bracket sequence (e.g. "()" or "()[]{}").
// Input: isValidBracketSequence("()[]{}")
// Output: true
// Explanation: The string contains a valid bracket sequence, so the function returns true.
// Input: isValidBracketSequence("(]")
// Output: false
// Explanation: The string contains an invalid bracket sequence, so the function returns false.
- Write a function twoDArray(arr) that rotates a two-dimensional array 90 degrees clockwise.
// Input: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
// Output: [[7, 4, 1], [8, 5, 2], [9, 6, 3]]
// Input: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
// Output: [[9, 5, 1], [10, 6, 2], [11, 7, 3], [12, 8, 4]]