Javascript Algorithm Examples Flashcards
How would you verify a prime number?
function isPrime(n){ var divisor = 2;
while (n > divisor){ if(n % divisor == 0){ return false; } else divisor++; } return true; }
> isPrime(137);
= true
isPrime(237);
= false
How could you find all prime factors of a number?
function primeFactors(n){ var factors = [], divisor = 2;
while(n>2){ if(n % divisor == 0){ factors.push(divisor); n= n/ divisor; } else{ divisor++; } } return factors; }
> primeFactors(69);
= [3, 23]
How do get nth Fibonacci number? - try 1
function fibonacci(n){ var fibo = [0, 1];
if (n <= 2) return 1;
for (var i = 2; i <=n; i++ ){ fibo[i] = fibo[i-1]+fibo[i-2]; }
return fibo[n];
}
> fibonacci(12);
= 144
How do get nth Fibonacci number? - try 2
function fibonacci(n){ if(n<=1) return n; else return fibonacci(n-1) + fibonacci (n-2); }
> fibonacci(12);
= 144
How would you find the greatest common divisor of two numbers?
function greatestCommonDivisor(a, b){ var divisor = 2, greatestDivisor = 1;
//if u pass a -ve number this will not work. fix it dude!! if (a < 2 || b < 2) return 1;
while(a >= divisor && b >= divisor){ if(a %divisor == 0 && b% divisor ==0){ greatestDivisor = divisor; } divisor++; } return greatestDivisor; }
> greatestCommonDivisor(14, 21);
=7
greatestCommonDivisor(69, 169);
= 1
How would you merge two sorted array?
function mergeSortedArray(a, b){ var merged = [], aElm = a[0], bElm = b[0], i = 1, j = 1;
if(a.length ==0) return b; if(b.length ==0) return a; /* if aElm or bElm exists we will insert to merged array (will go inside while loop) to insert: aElm exists and bElm doesn't exists or both exists and aElm < bElm this is the critical part of the example */ while(aElm || bElm){ if((aElm && !bElm) || aElm < bElm){ merged.push(aElm); aElm = a[i++]; } else { merged.push(bElm); bElm = b[j++]; } } return merged; }
> mergeSortedArray([2,5,6,9], [1,2,3,29]);
= [1, 2, 2, 3, 5, 6, 9, 29]
How would you swap two numbers without using a temporary variable?
function swapNumb(a, b){
console.log(‘before swap: ‘,’a: ‘, a, ‘b: ‘, b);
b = b -a;
a = a+ b;
b = a-b;
console.log(‘after swap: ‘,’a: ‘, a, ‘b: ‘, b);
}
> swapNumb(2, 3);
= before swap: a: 2 b: 3
= after swap: a: 3 b: 2
How would you reverse a string in JavaScript? - try 1
function reverse(str){ var rtnStr = ''; for(var i = str.length-1; i>=0;i--){ rtnStr +=str[i]; } return rtnStr; }
> reverse(‘you are a nice dude’);
= “edud ecin a era uoy”
How would you reverse a string in JavaScript? - try 2
function reverse(str){ var rtnStr = []; if(!str || typeof str != 'string' || str.length < 2 ) return str;
for(var i = str.length-1; i>=0;i--){ rtnStr.push(str[i]); } return rtnStr.join(''); }
How would you reverse a string in JavaScript? - try 3
function reverse(str) { str = str.split(''); var len = str.length, halfIndex = Math.floor(len / 2) - 1, revStr; for (var i = 0; i <= halfIndex; i++) { revStr = str[len - i - 1]; str[len - i - 1] = str[i]; str[i] = revStr; } return str.join(''); }
How would you reverse words in a sentence?
//have a tailing white space //fix this later //now i m sleepy function reverseWords(str){ var rev = [], wordLen = 0; for(var i = str.length-1; i>=0; i--){ if(str[i]==' ' || i==0){ rev.push(str.substr(i,wordLen+1)); wordLen = 0; } else wordLen++; } return rev.join(' '); }
If you have a string like “I am the good boy”. How can you generate “I ma eht doog yob”? Please note that the words are in place but reverse.
function reverseInPlace(str){ return str.split(' ').reverse().join(' ').split('').reverse().join(''); }
> reverseInPlace(‘I am the good boy’);
= “I ma eht doog yob”
How will you verify a word as palindrome?
function isPalindrome(str){ var i, len = str.length; for(i =0; i isPalindrome('madam') = true > isPalindrome('toyota') = false
If you have a function that generate random number between 1 to 5 how could u generate random number 1 to 7 by using that function?
function rand5(){ return 1 + Math.random() * 4; }
function rand7(){ return 5 + rand5() / 5 * 2; }
from a unsorted array of numbers 1 to 100 excluding one number, how will you find that number
function missingNumber(arr){ var n = arr.length+1, sum = 0, expectedSum = n* (n+1)/2;
for(var i = 0, len = arr.length; i < len; i++){ sum += arr[i]; }
return expectedSum - sum;
}
> missingNumber([5, 2, 6, 1, 3]);
= 4