3 common Algorithm Questions.

Veronika Dodda
3 min readDec 18, 2020

While preparing for my future tech interviews I started the Udemy course on Algorithms and Data structures. In today's’ blog, I’d like to show 3 solutions to common algorithm questions.

  1. FizzBuzz.

Directions:

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

Example:  fizzBuzz(5);
1
2
fizz
4
buzz

Solution:

function fizzBuzz(n){     for (let i=1; i<= n; i++){     
if (i % 3 === 0 && i % 5 === 0){
console.log('fizzbuzz')
} else if (i % 3 === 0){
console.log('fizz')
} else if ( i% 5 === 0){
console.log('buzz')
} else {
console.log(i)
}
}
}

2. Anagrams.

Directions:
Check to see if two provided strings are anagrams of eachother. 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
— — Examples
anagrams(‘rail safety’, ‘fairy tales’) → True
anagrams(‘RAIL! SAFETY!’, ‘fairy tales’) → True
anagrams(‘Hi there’, ‘Bye there’) → False

1st Solutions:

unction anagrams(stringA, stringB) { 
return cleanString(stringA)=== cleanString(stringB)
}
function cleanString(str){
return str
.replace(/[^\w]/g,'')
.toLowerCase()
.split('')
.sort()
.join('')
}

2nd Solution:

function anagrams (stringA, stringB){const aCharMap = buildCharMap(stringA)
const bCharMap = buildCharMap(stringB)
if (Object.keys(aCharMap).length !== Object.keys(bCharMap).length){
return false
}
for (let char in aCharMap){
if (aCharMap[char] !== bCharMap[char]){
return false
}
}
return true
}
function buildCharMap(str){
const charMap = {}
for (let char of str.replace(/[^\w]/g, "").toLowerCase()){
charMap[char] = charMap[char]+1 || 1;
}
return charMap
}

3. Steps:

Directions:
Write a function that accepts a positive number N.
The function should console log a step shape
with N levels using the # character. Make sure the
step has spaces on the right-hand side!
— — Examples
steps(2)
‘# ‘
‘##’
steps(3)
‘# ‘
‘## ‘
‘###’
steps(4)
‘# ‘
‘## ‘
‘### ‘
‘####’

1st Solution:

function steps(n) {// From 0 to n (iterate througt rows)
//Create an empty string, 'stair'
//From 0 to n (iterate througt columns)
//If the current column is equal to or less that the current row
//Add a '#' to 'stair'
//Else
//Add space to 'stair'
//Console log 'stair'
for (let row = 0; row < n; row++){
let stair = '';
for (let column = 0; column < n; column++){
if (column <= row){
stair += '#'
}else {
stair += ' '
}
}
console.log(stair)
}
}

2nd Solutons:

function steps(n, row = 0, stair = ''){
//If (row ===n) then we have hit the end of our problem
//If the 'stair' string has a length === n then we are at the end of row
//If the length of the stair string is less that or equal to the row number
// we are working on, we add '#', otherwise add as space
if (n === row){
return;
}
if ( n === stair.length){
console.log(stair);
return steps(n, row + 1);
}
if (stair.string <= row){
stair +='#'
} else {
stair +=' '
}
steps(n, row, stair)
}

Have fun leaning Algorithms!

--

--