Difficulty
Very easy


Concatenation of Array (opens in a new tab)

  • Create a new array to hold the concatenated array
  • Loop through the input array and push each element to the new array twice
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var getConcatenation = function (nums) {
  const concatNums = [];
 
  for (let i = 0; i < nums.length; i++) {
    concatNums.push(nums[i]);
  }
 
  for (let i = 0; i < nums.length; i++) {
    concatNums.push(nums[i]);
  }
 
  return concatNums;
};
Complexity
  • Time: O(n)
  • Space: O(n)


Missing Number (opens in a new tab)

  • The total of the first n natural numbers is n * (n + 1) / 2
  • The difference between the sum of the input array and the total should be the missing number
/**
 * @param {number[]} nums
 * @return {number}
 */
var missingNumber = function (nums) {
  const n = nums.length;
 
  var total = (n * (n + 1)) / 2;
  var sum = 0;
 
  for (var i = 0; i < n; i++) {
    sum = sum + nums[i];
  }
 
  return total - sum;
};
Complexity
  • Time: O(n)
  • Space: O(1)


Single Number (opens in a new tab)

  • Perform bitwise XOR on all elements in the array
  • The XOR operation will cancel out all duplicate numbers, leaving the single number
/**
 * @param {number[]} nums
 * @return {number}
 */
var singleNumber = function (nums) {
  return nums.reduce((x, y) => x ^ y);
};
Complexity
  • Time: O(n)
  • Space: O(1)


Kids With the Greatest Number of Candies (opens in a new tab)

  • Find the kid with the most candies & check if each kid can have the most candies by adding extra candies
  • Max can also be easily found using Math.max(...candies)
/**
 * @param {number[]} candies
 * @param {number} extraCandies
 * @return {boolean[]}
 */
var kidsWithCandies = function (candies, extraCandies) {
  // find the kid with the most candies
  let max = 0;
  for (let i = 0; i < candies.length; i++) {
    if (max < candies[i]) {
      max = candies[i];
    }
  }
 
  // check if each kid can have the most candies
  for (let i = 0; i < candies.length; i++) {
    candies[i] = candies[i] + extraCandies >= max;
  }
 
  return candies;
};
Complexity
  • Time: O(n)
  • Space: O(n)


Count Tested Devices After Test Operations (opens in a new tab)

  • Keep track of the number of devices that have been tested
  • Increment the count if the battery percentage is greater than the current count
/**
 * @param {number[]} batteryPercentages
 * @return {number}
 */
var countTestedDevices = function (batteryPercentages) {
  let count = 0;
 
  for (let i = 0; i < batteryPercentages.length; i++) {
    if (batteryPercentages[i] > count) {
      count++;
    }
  }
 
  return count;
};
Complexity
  • Time: O(n)
  • Space: O(1)


Remove Duplicates from Sorted Array (opens in a new tab)

  • Use 2 pointers to keep track of the current and next unique element
  • Iterate through the array and update the next unique element when a new unique element is found
/**
 * @param {number[]} nums
 * @return {number}
 */
var removeDuplicates = function (nums) {
  let pointer = -1;
 
  for (let i = 0; i < nums.length; i++) {
    if (nums[pointer] !== nums[i]) {
      // Found a new unique element
      pointer++;
      nums[pointer] = nums[i];
    }
  }
 
  return pointer + 1; // Return the length of the unique elements
};
Complexity
  • Time: O(n)
  • Space: O(1)


Palindrome Number (opens in a new tab)

  • Build a number starting with the last digit of the input number using modulo and division
  • Compare the reversed number with the input number
/**
 * @param {number} x
 * @return {boolean}
 */
var isPalindrome = function (x) {
  if (x < 0) {
    // if negative number
    return false;
  }
 
  let reverse = 0, value = x;
  while (value > 0) {
    const remainder = value % 10; // get the last digit
    reverse = remainder + reverse * 10; // build the reversed number
    value = Math.floor(value / 10); // remove the last digit
  }
 
  return reverse === x;
};
Complexity
  • Time: O(digits)
  • Space: O(1)


Reverse Integer (opens in a new tab)

  • Reverse the number without sign and check for overflow
  • Use the sign to return the reversed number with the correct sign
/**
 * @param {number} x
 * @return {number}
 */
var reverse = function (x) {
  const limit = Math.pow(2, 31);
  const isPositive = x > 0;
 
  let value = Math.abs(x); // remove the sign
  let reverse = 0;
  while (value > 0) {
    const remainder = value % 10;
    value = Math.floor(value / 10);
    reverse = remainder + 10 * reverse;
  }
 
  // check for overflow
  if (isPositive && reverse >= limit) {
    return 0;
  } else if (!isPositive && reverse > limit) {
    return 0;
  }
 
  // return the reversed number with the correct sign
  return isPositive ? reverse : -reverse;
};
Complexity
  • Time: O(digits)
  • Space: O(1)


Reverse String (opens in a new tab)

  • Use two pointers to swap the items between left and right
  • Continue swapping until left is less than right
/**
 * @param {character[]} s
 * @return {void} Do not return anything, modify the string in-place instead
 */
var reverseString = function (s) {
  const length = s.length;
 
  for (let left = 0, right = length - 1; left < right; left++, right--) {
    // swap the items between left and right
    const temp = s[left];
    s[left] = s[right];
    s[right] = temp;
  }
};
Complexity
  • Time: O(n)
  • Space: O(1)


Running Sum of 1d Array (opens in a new tab)

  • Naive approach would be to calculate the sum of all the array elements before the current index for every element, in to a new array.
  • Simpler and better approach would be to replace the current element by its sum with the previous element
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var runningSum = function (nums) {
  for (let i = 1; i < nums.length; i++) {
    nums[i] += nums[i - 1];
  }
  return nums;
};
Complexity
  • Time: O(n)
  • Space: O(1)


Shuffle the Array (opens in a new tab)

  • Simple solution would be to create a new array and shift the elements in the order given
/**
 * @param {number[]} nums
 * @param {number} n
 * @return {number[]}
 */
var shuffle = function (nums, n) {
  const newNums = [];
 
  for (let i = 0, j = n; i < n; i++, j++) {
    newNums.push(nums[i], nums[j]);
  }
 
  return newNums;
};
Complexity
  • Time: O(n)
  • Space: O(n)


Fibonacci Number (opens in a new tab)

  • A good approach would be calculate the nth fibonacci number either recursively or iteratively
  • As we know nth fibonacci number is sum of n-1 th and n-2th fibonacci numbers, we can start from 1st position till we calculate nth.
/**
 * @param {number} n
 * @return {number}
 */
var fib = function (n) {
  // initialize 0th and 1st fibonacci numbers
  let fib1 = 0, fib2 = 1;
 
  for (let i = 1; i <= n; i++) {
    temp = fib2;
    fib2 = fib1 + fib2; // next fibonacci number is sum of current and next
    fib1 = temp; // current fibonacci number is next fibonacci number
  }
 
  return fib1;
};
Complexity
  • Time: O(n)
  • Space: O(1)


Binary Search (opens in a new tab)

  • Binary search is a search algorithm that finds the position of a target value within a sorted array
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function (nums, target) {
  let left = 0, right = nums.length - 1;
 
  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
 
    if (nums[mid] === target) {
      return mid;
    } else if (nums[mid] > target) {
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }
 
  return -1;
};
Complexity
  • Time: O(log(n))
  • Space: O(1)


Check If Two String Arrays are Equivalent (opens in a new tab)

  • Having 2 array, convert array into string, with one by one check if every character matches return true else false
/**
 * @param {string[],string[]} nums
 * @return {boolean}
 */
const stringComparison = (word1, word2) => {
  let word1String = word1.join('');
  let word2String = word2.join('');
  if (word1String?.length != word2String?.length) return false;
 
  for (let i = 0; i < word1String?.length; i++) {
    if (word1String[i] != word2String[i]) {
      return false;
    }
  }
  return true;
};
Complexity
  • Time: O(n)
  • Space: O(1)


Max Consecutive Ones (opens in a new tab)

  • Use a counter to keep track of the number of consecutive ones
  • Update the maximum count when a zero is encountered
/**
 * @param {number[]} nums
 * @return {number}
 */
var findMaxConsecutiveOnes = function (nums) {
  let count = 0, max = 0;
 
  for (const num of nums) {
    if (num) {
      count++;
    } else {
      max = Math.max(max, count);
      count = 0;
    }
  }
 
  return Math.max(max, count); // Edge case when the array ends with 1
};
Complexity
  • Time: O(n)
  • Space: O(1)