Based on teachings at https://www.udemy.com/coding-interview-bootcamp-algorithms-and-data-structure/

1

Given an array and chunk size, divide the array into many subarrays where each subarray is of length size.


Use splice() and recursion for this solution.


Code provided:

function chunk(array, size) {
 // Add code here
}
function chunk(array, size, result) {
 result = result || [];
 if (array.length > 0) {
  result.push(array.splice(0, size));
  return chunk(array, size, result);
 } else {
  return result;
 }
}

// Note: The splice() method will mutate the original array, which is usually undesired.
2

What are the different ways to reverse a string?

reverse(), for loop, for-of loop, reduce(), reduceRight(), forEach()

3

Check if a string is a palindrome (using the variable str as the input)


Note: A palindrome is a word that is the same when in reverse.


Code provided:

function isPalindrome(str) {
 // Add code here
}
function isPalindrome(str) {
  return str === str.split('').reverse().join('');
}
4

Reverse a string using the reduce() method (using the variable str as the input)


Code provided:

function reverse(str) {
 // Add code here
}
function reverse(str) {
  return str.split('').reduce((reversedString, letter) => letter + reversedString, '');
}
5

Reverse a string using the reduceRight() method (using the variable str as the input)


Code provided:

function reverse(str) {
 // Add code here
}
function reverse(str) {
  return str.split('').reduceRight((reversedString, letter) => reversedString + letter, '');
}
6

Reverse a string using a for loop (using the variable str as the input)


Code provided:

function reverse(str) {
 // Add code here
}
function reverse(str) {
  let reversedString = '';
  for (let i = str.length - 1; i >= 0; i--) {
    reversedString += str[i];
  }
  return reversedString;
}
7

Write a program that console logs the numbers from 1 to num. But for multiples of three print “fizz” instead of the number and for the multiples of five print “buzz”. For numbers which are multiples of both three and five print “fizzbuzz”.


Code provided:

function fizzBuzz(num) {
 // Add code here
}
function fizzBuzz(num) {
  for (let i = 1; i <= num; i++) {
    if (i % 3 === 0 && i % 5 === 0) {
     console.log('fizzbuzz');
     continue;
    }

    if (i % 3 === 0) {
     console.log('fizz');
     continue;
    }

    if (i % 5 === 0) {
     console.log('buzz');
     continue;
    }
  
    console.log(i);
  }
}
8

Check to see if two provided strings (stringA and stringB) are anagrams of each other. One string is an anagram of another if it uses the same characters in the same quantity. Only consider characters, not spaces or punctuation. Consider capital letters to be the same as lower case.


Use sort() for this solution.


Code provided:

function anagrams(stringA, stringB) {
 // Add code here
}
function formatWord(word) {
 return word.replace(/[^\w]/g,'').toLowerCase();
}

function anagrams(stringA, stringB) {
 const formattedStringA = formatWord(stringA);
 const formattedStringB = formatWord(stringB);
 const sortedA = formattedStringA.split('').sort().join('');
 const sortedB = formattedStringB.split('').sort().join('');

 return sortedA === sortedB;
}
9

Given an array and chunk size, divide the array into many subarrays where each subarray is of length size.


Use slice() and a for loop for this solution.


Code provided:

function chunk(array, size) {
 // Add code here
}
function chunk(array, size) {
  let newArray = [];
  for (let index = 0; index < array.length; index += size) {
    newArray.push(array.slice(index, index + size));
  }
  return newArray;
}
10

Reverse a string using a for-of loop (using the variable str as the input)


Code provided:

function reverse(str) {
 // Add code here
}
function reverse(str) {
  let reversedString = '';
  for (let letter of str) {
    reversedString = letter + reversedString;
  }
  return reversedString;
}
11

Given an array and chunk size, divide the array into many subarrays where each subarray is of length size.


Use slice() and a while loop for this solution.


Code provided:

function chunk(array, size) {
 // Add code here
}
function chunk(array, size) {
  let newArray = [];
  let index = 0;
  while (index < array.length) {
   newArray.push(array.slice(index, index + size));
   index += size;
  }
  return newArray;
}
12

Given an array and chunk size, divide the array into many subarrays where each subarray is of length size.


Use splice() and a while loop for this solution.


Code provided:

function chunk(array, size) {
 // Add code here
}
function chunk(array, size) {
  let newArray = [];
  while (array.length > 0) {
   newArray.push(array.splice(0, size));
  }
  return newArray;
}

// Note: The splice() method will mutate the original array, which is usually undesired.
13

Reverse a string using a forEach() loop (using the variable str as the input)


Code provided:

function reverse(str) {
 // Add code here
}
function reverse(str) {
  let reversedString = '';
  str.split('').forEach((letter) => {
   reversedString = letter + reversedString;
  });
  return reversedString;
}
14

Reverse a number (using the variable num as the input)


Note: Make sure it reverses the following numbers as specified:

  • 15 -> 51
  • 981 -> 189
  • 500 -> 5
  • -15 -> -51
  • -90 -> -9


Code provided:

function reverseNum(num) {
 // Add code here
}
function reverseNum(num) {
  const reversedNumberString = num
    .toString()
    .split('')
    .reverse()
    .join('');

  return parseInt(reversedNumberString) * Math.sign(num);
}

// Math.sign() returns a number representing the sign of a specified number
// parseInt() returns a number from a string and removes any leading zeros as well as any negative signs that are at the end of the string after reversing it
// toString() returns a string representing a number
15

Check to see if two provided strings (stringA and stringB) are anagrams of each other. One string is an anagram of another if it uses the same characters in the same quantity. Only consider characters, not spaces or punctuation. Consider capital letters to be the same as lower case.


Use a character map for this solution.


Code provided:

function anagrams(stringA, stringB) {
 // Add code here
}
function formatWord(word) {
 return word.replace(/[^\w]/g,'').toLowerCase();
}

function getCharMap(word) {
 let charMap = {};
 for (letter of word) {
  charMap[letter] = charMap[letter] + 1 || 1;
 }
 return charMap;
}

function anagrams(stringA, stringB) {
 const formattedStringA = formatWord(stringA);
 const formattedStringB = formatWord(stringB);
 const charMapA = getCharMap(formattedStringA);
 const charMapB = getCharMap(formattedStringB);

 if (Object.keys(charMapA).length !== Object.keys(charMapB).length) {
  return false;
 }

 for (let letter in charMapA) {
  if (charMapA[letter] !== charMapB[letter]) {
   return false;
  }
 }

 return true;
}
16

Determine which character is the most common in a string (using the variable str as the input)


Code provided:

function maxChar(str) {
 // Add code here
}
function maxChar(str) {
  const charMap = {};
  let maxChar = {char: '', count: 0};

  for (let letter of str) {
    charMap[letter] = charMap[letter] + 1 || 1;
  }

  for (const letter in charMap) {
    const letterCount = charMap[letter];

    if (maxChar.count < letterCount) {
      maxChar = {
        char: letter,
        count: letterCount
      };
    }
  }

  return maxChar.char;
}

// Strategy:
// 1 - Create a character map that uses each letter for the keys and the number of times a letter appears as the values
// 2 - Loop through the character map and compare it to the previous character count
17

Write a function that accepts a string. The function should capitalize the first letter of each word in the string then return the capitalized string.


Use split() and map() for this solution.


Code provided:

function capitalize(string) {
 // Add code here
}
function capitalize(string) {
  return string
    .split(' ')
    .map(function(word) {
      return word[0].toUpperCase() + word.slice(1);
    })
    .join(' ');
}
18

Write a function that accepts a string. The function should capitalize the first letter of each word in the string then return the capitalized string.


Use map() and if() for this solution.


Code provided:

function capitalize(string) {
 // Add code here
}
function capitalize(string) {
 return string.split('').map(function(character, index) {
  if (index === 0 || str[index - 1] === ' ') {
   character.toUpperCase();
  }

  return character;
 }).join('');
}
19

Write a function that accepts a positive number desiredRowsNum. The function should console log a step shape with desiredRowsNum levels using the # character. Make sure the step has spaces on the right hand side!


Use recursion for this solution.


Example:

printSteps(3);
      '#  '
      '## '
      '###'


Code provided:

function printSteps(desiredRowsNum) {
 // Add code here
}
function steps(desiredRowsNum, rowNum = 1, rowChars = '') {
  if (rowNum > desiredRowsNum) {
   return;
  }

  if (desiredRowsNum === rowChars.length) {
    console.log(rowChars);
    return steps(desiredRowsNum, rowNum + 1);
  }

  rowChars += rowChars.length < rowNum ? '#' : ' ';
  steps(desiredRowsNum, rowNum, rowChars);
}


// Strategy:

// Create a base condition that will end the recursive loop
  // Base case - If this condition is met, stop the recursive loop
  // If the current row iteration is greater than the desired number of rows, stop the loop

// If the number of characters in the built string are equal to the number of desired rows, then we know it is ready to print it
// Re-run the function and increment the row counter. The function call must be returned so the rest of the function isn't executed

// If the built string hasn't reached the number of row iteration, then another pound sign must be added
// If the built string has reached the number of row iteration, then a space sign must be added

// Re-run the function using the same row iteration counter so that the remaining pound or space characters are added
20

Write a function that accepts a positive number desiredRowsNum. The function should console log a step shape with desiredRowsNum levels using the # character. Make sure the step has spaces on the right hand side!


Use a nested for loop inside another for loop for this solution.


Example:

printSteps(3);
      '#  '
      '## '
      '###'


Code provided:

function printSteps(desiredRowsNum) {
 // Add code here
}
function printSteps
(desiredRowsNum) {
  for (let rowNum = 1; rowNum <= desiredRowsNum; rowNum++) {
    let rowChars = '';
    for (let columnNum = 1; columnNum <= desiredRowsNum; columnNum++) {
      rowChars += columnNum <= rowNum ? '#' : ' ';
    }
    console.log(rowChars);
  }
}
21

Write a function that prints out all numbers from num down to 1.


Use recursion for this solution.


Code provided:

function printNumber(num) {
 // Add code here
}
function printNumber(n) {
  if (n === 0) {
   return;
  }  

  console.log(n);
  return printNumber(n - 1);
}
22

Write a function that accepts a positive number desiredRowsNum. The function should console log a step shape with desiredRowsNum levels using the # character. Make sure the step has spaces on the right hand side!


Use repeat() and padEnd() for this solution.


Example:

printSteps(3);
      '#  '
      '## '
      '###'


Code provided:

function printSteps(desiredRowsNum) {
 // Add code here
}


function printSteps
(desiredRowsNum
) {
  for (let i = 1; i <= desiredRowsNum
; i++) {
    console.log('#'.repeat(i).padEnd(desiredRowsNum
))
  }
}
23

Reverse a string using the reverse() method (using the variable str as the input)


Code provided:

function reverse(str) {
 // Add code here
}
function reverse(str) {
  return str.split('').reverse().join('');
}