Write a function that finds and returns the largest element from an array.
function findMax(arr) {
let max = arr[0];
for (let num of arr) {
if (num > max) {
max = num;
}
}
return max;
}
console.log(findMax([10, 5, 20, 8])); // 20This solution initializes the first element as the maximum value and iterates through the array. If a larger element is found, it updates the maximum. The time complexity is O(n) and the space complexity is O(1).
Write a function that finds and returns the smallest element from an array.
function findMin(arr) {
let min = arr[0];
for (let num of arr) {
if (num < min) {
min = num;
}
}
return min;
}
console.log(findMin([10, 5, 20, 1])); // 1This solution initializes the first element as the minimum value and iterates through the array. If a smaller element is found, it updates the minimum. The time complexity is O(n) and the space complexity is O(1).
Write a function that reverses the elements of an array without using the built-in reverse() method.
function reverseArray(arr) {
let left = 0;
let right = arr.length - 1;
while (left < right) {
[arr[left], arr[right]] = [arr[right], arr[left]];
left++;
right--;
}
return arr;
}
console.log(reverseArray([1, 2, 3, 4]));const arr = [1, 2, 3, 4];
console.log(arr.reverse());Output:
[4, 3, 2, 1];This solution uses the two-pointer approach. One pointer starts from the beginning and another from the end of the array. Elements are swapped until both pointers meet. The time complexity is O(n) and the space complexity is O(1).
Write a function to reverse a string without using built-in methods like reverse().
function reverseString(str) {
let reversed = "";
for (let i = str.length - 1; i >= 0; i--) {
reversed += str[i];
}
return reversed;
}
console.log(reverseString("hello")); // ollehfunction reverseString(str) {
let chars = [];
for (let i = 0; i < str.length; i++) {
chars[i] = str[i];
}
let left = 0;
let right = chars.length - 1;
while (left < right) {
let temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
let result = "";
for (let char of chars) {
result += char;
}
return result;
}
console.log(reverseString("hello"));To reverse a string without using built-in methods, iterate from the last character to the first character and keep appending each character to a new string. The time complexity is O(n) and the space complexity is O(n).
Write a function to check whether a given string is a palindrome.
A palindrome is a word that reads the same forward and backward.
"madam" => true
"racecar" => true
"hello" => falsefunction isPalindrome(str) {
let left = 0;
let right = str.length - 1;
while (left < right) {
if (str[left] !== str[right]) {
return false;
}
left++;
right--;
}
return true;
}
console.log(isPalindrome("madam"));A palindrome is a string that reads the same forward and backward. Using the two-pointer approach, we compare characters from both ends of the string. If any pair does not match, we return false; otherwise, the string is a palindrome. The time complexity is O(n) and the space complexity is O(1).
Write a function to count the number of vowels in a given string.
Vowels are:
(a, e, i, o, u);"JavaScript" => 3function countVowels(str) {
let count = 0;
let vowels = "aeiou";
for (let char of str.toLowerCase()) {
if (vowels.includes(char)) {
count++;
}
}
return count;
}
console.log(countVowels("JavaScript")); // 3This solution iterates through each character of the string and checks whether it is a vowel. If the character is a vowel, the count is incremented. The time complexity is O(n) and the space complexity is O(1).
Given an array of integers nums and an integer target, return the indices of the two numbers such that they add up to the target.
You may assume that each input has exactly one solution.
nums = [2, 7, 11, 15];
target = 9;
Output: [0, 1];Because:
nums[0] + nums[1] = 2 + 7 = 9function twoSum(nums, target) {
let map = {};
for (let i = 0; i < nums.length; i++) {
let diff = target - nums[i];
if (map[diff] !== undefined) {
return [map[diff], i];
}
map[nums[i]] = i;
}
}
console.log(twoSum([2, 7, 11, 15], 9)); // [0, 1]This solution uses a HashMap to store previously seen numbers and their indices. For each number, we calculate the required complement (
target - currentNumber). If the complement already exists in the HashMap, we return the indices. This approach has a time complexity of O(n) and a space complexity of O(n).
Write a function to count the frequency of each character in a string.
The function should return an object where:
- Key = Character
- Value = Number of occurrences
"apple";Output:
{
a: 1,
p: 2,
l: 1,
e: 1
}function frequency(str) {
let map = {};
for (let char of str) {
map[char] = (map[char] || 0) + 1;
}
return map;
}
console.log(frequency("apple"));
// { a: 1, p: 2, l: 1, e: 1 }This solution uses a HashMap (JavaScript object) to store the count of each character. While iterating through the string, if the character already exists, its count is incremented; otherwise, it is initialized to 1. The time complexity is O(n) and the space complexity is O(n).
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An anagram is a word formed by rearranging the letters of another word using all the original letters exactly once.
s = "listen";
t = "silent";
Output: true;Because both strings contain the same characters with the same frequency.
function isAnagram(s, t) {
if (s.length !== t.length) return false;
let map = {};
for (let char of s) {
map[char] = (map[char] || 0) + 1;
}
for (let char of t) {
if (!map[char]) return false;
map[char]--;
}
return true;
}
console.log(isAnagram("listen", "silent")); // trueThis solution uses a HashMap to count the frequency of characters in the first string. Then it iterates through the second string and decreases the corresponding counts. If any character is missing or the counts do not match, it returns false. If all counts become zero, the strings are anagrams. The time complexity is O(n) and the space complexity is O(n).
Given a sorted array of integers and a target value, return the indices of the two numbers whose sum equals the target.
The array is already sorted in ascending order.
arr = [1, 2, 3, 4, 6];
target = 6;
Output: [1, 3];Because:
arr[1] + arr[3] = 2 + 4 = 6function twoSumSorted(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left < right) {
let sum = arr[left] + arr[right];
if (sum === target) {
return [left, right];
}
if (sum < target) {
left++;
} else {
right--;
}
}
}
console.log(twoSumSorted([1, 2, 3, 4, 6], 6)); // [1, 3]This solution uses the Two Pointer approach. One pointer starts from the beginning of the sorted array and the other from the end. If the current sum is smaller than the target, the left pointer is moved forward. If the sum is larger, the right pointer is moved backward. This achieves a time complexity of O(n) and a space complexity of O(1).
Given a sorted array nums, remove the duplicates in-place such that each unique element appears only once.
Return the number of unique elements.
nums = [1, 1, 2, 2, 3, 4, 4];
Output: 4;Unique elements:
[1, 2, 3, 4];function removeDuplicates(nums) {
let i = 0;
for (let j = 1; j < nums.length; j++) {
if (nums[i] !== nums[j]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
console.log(removeDuplicates([1, 1, 2, 2, 3, 4, 4])); // 4This solution uses the Two Pointer approach. Pointer
itracks the position of the last unique element, while pointerjscans the array. Whenever a new unique element is found, it is placed at the next position ofi. The function returns the count of unique elements. The time complexity is O(n) and the space complexity is O(1).
Given an array of integers and a number k, find the maximum sum of any contiguous subarray of size k.
arr = [2, 1, 5, 1, 3, 2];
k = 3;
Output: 9;Because:
[2, 1, 5] = 8
[1, 5, 1] = 7
[5, 1, 3] = 9
[1, 3, 2] = 6Maximum sum = 9
function maxSum(arr, k) {
let windowSum = 0;
for (let i = 0; i < k; i++) {
windowSum += arr[i];
}
let max = windowSum;
for (let i = k; i < arr.length; i++) {
windowSum += arr[i] - arr[i - k];
max = Math.max(max, windowSum);
}
return max;
}
console.log(maxSum([2, 1, 5, 1, 3, 2], 3)); // 9This solution uses the Sliding Window technique. First, it calculates the sum of the first
kelements. Then it slides the window by adding the new element and removing the old element from the sum. This avoids recalculating the entire window each time. The time complexity is O(n) and the space complexity is O(1).
Given a string str, find the length of the longest substring without repeating characters.
A substring is a continuous sequence of characters.
str = "abcabcbb";
Output: 3;Because:
"abc";is the longest substring without repeating characters.
function lengthOfLongestSubstring(str) {
let chars = {};
let left = 0;
let max = 0;
for (let right = 0; right < str.length; right++) {
while (chars[str[right]]) {
chars[str[left]] = false;
left++;
}
chars[str[right]] = true;
let currentLength = right - left + 1;
if (currentLength > max) {
max = currentLength;
}
}
return max;
}
console.log(lengthOfLongestSubstring("abcabcbb")); // 3This solution uses the Sliding Window technique with two pointers. An object is used to track characters currently in the window. When a duplicate character is found, the window is shrunk from the left until the duplicate is removed. The maximum window length found during the traversal is returned. The time complexity is O(n) and the space complexity is O(n).
Given a string containing only parentheses (), curly braces {}, and square brackets [], determine whether the input string is valid.
A string is valid if:
- Open brackets are closed by the same type of brackets.
- Open brackets are closed in the correct order.
- Every closing bracket has a corresponding opening bracket.
str = "()[]{}";
Output: true;str = "([)]";
Output: false;function isValid(str) {
let stack = [];
let map = {
")": "(",
"}": "{",
"]": "[",
};
for (let char of str) {
if ("([{".includes(char)) {
stack.push(char);
} else {
if (stack.pop() !== map[char]) {
return false;
}
}
}
return stack.length === 0;
}
console.log(isValid("()[]{}")); // true
console.log(isValid("([)]")); // falseThis solution uses a Stack data structure. Opening brackets are pushed onto the stack. When a closing bracket is encountered, the top element of the stack is checked to ensure it matches the corresponding opening bracket. If all brackets are matched correctly and the stack is empty at the end, the string is valid. The time complexity is O(n) and the space complexity is O(n).