0% found this document useful (0 votes)
336 views

Udemy - The Coding Interview Bootcamp Algorithms

The document contains code snippets for array chunking, checking if strings are anagrams, capitalizing the first letter of words in a string, printing step patterns, printing a pyramid pattern, reversing integers, finding vowels in a string, calculating the Fibonacci series, solving the two-sum problem, adding up numbers in a range, reversing arrays, searching arrays, and calculating the area of a triangle.

Uploaded by

Sambeet Sahoo
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
336 views

Udemy - The Coding Interview Bootcamp Algorithms

The document contains code snippets for array chunking, checking if strings are anagrams, capitalizing the first letter of words in a string, printing step patterns, printing a pyramid pattern, reversing integers, finding vowels in a string, calculating the Fibonacci series, solving the two-sum problem, adding up numbers in a range, reversing arrays, searching arrays, and calculating the area of a triangle.

Uploaded by

Sambeet Sahoo
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Array Chunking

Anagrams
First Lette Capital
Steps
Pyramid
Interger Reversal
Find Vowels
Fibonacie
Two sum
Add Up Numbers // 5 means 5+4+3+2+1

Array Chunking

let arr1 = [1,2,3,4,5,6,7,8,9,10]


function arrayChunk(arr,size){
var result = [];
for(i=0;i<arr.length;i+=size){
result.push(arr.slice(i,i+size))
}
return result;
}

-----------------
Anagrams

let str1 = "Hello There!";


let str2 = "There! Hello!!!";

function anagrams(st1,st2){
return cleanString(st1) === cleanString(st2);
}

function cleanString(string){
return string.replace(/[^\w]/gi,'').toLowerCase().split('').sort().join('');
}

-----------------
First Lette Capital

let str1 = "look, it is working";

function capitalize(string){
let wordsArr = string.split(" ");
var res = [];
for(i=0;i<wordsArr.length;i++){
res.push(wordsArr[i].charAt(0).toUpperCase() + wordsArr[i].slice(1));
}
return res.join(' ');
}

or

function capFirst(str){
let wordsArr = str.split(" ");
let x = wordsArr.map(function(el){
return el.charAt(0).toUpperCase() + el.slice(1);
})
return x;
}

------------------
Steps

function steps(n){
for(row=0;row<n;row++){
var patt = "";
for(col=0;col<n;col++){
if(col<=row){
patt += "*";
}
else{
patt += " ";
}
}
console.log(patt);
}
}

steps(5)

*
**
***
****
*****

if we do if(row >= col)

*****
****
***
**
*

-----------------
Pyramid

function pyramid(n){
let midpoint = Math.floor( (2*n-1)/2 );
for(row=0; row<n; row++){
let patt = "";
for(col=0; col<2*n-1; col++){
if( midpoint - row <= col && midpoint + row >= col ){
patt += "*";
}else{
patt += " ";
}
}
console.log(patt);
}
}

------------------
Interger Reversal
function integerReversal(n){
let rev = Number(String(Math.abs(n)).split('').reverse().join(''));
return n < 1 ? -1 * rev : rev;
}

------------------
Find Vowels

var str1 = "Hi there";


function findVowels(str){
let vow = ["a","e","i","o","u"];
let res = [];
for(el of str.toLowerCase()){
if(vow.includes(el)){
res.push(el);
}
}
return res;
}

------------------
Fibonacie

series is 1, 1, 2, 3, 5, 8, 13

function fib(n){
if(n<2){
return n;
}
return fib(n-1) + fib(n-2);
}

------------------
//Two sum

function twoSum(arr,sum){
let res = [];
let hashtable = [];
for(i=0;i<arr.length;i++){
let currentEl = arr[i];
let counterEl = sum - currentEl;
if(hashtable.indexOf(counterEl) !== -1){
res.push([currentEl,counterEl]);
}
hashtable.push(currentEl);
}
return res;
}

//twoSum([1, 6, 4, 5, 3, 3], 7);

function addUp(num) {
if(num == 1) return 1;
else return num + addUp(num-1)
}
// Reverse Array

function reverse(arr) {
let revArr = [];
for(i=arr.length;i>=0;i--){
revArr.push(arr[i])
}
revArr.shift();
return revArr;
}

// write a function
search([1, 2, 3, 4], 3) ➞ 2

function search(arr, item) {


for(i=0;i<arr.length;i++){
if(arr[i] === item){
return i;
}
}
return -1;
}

or

function search(arr, item) {


return item ? arr.indexOf(item) : -1
}

Number index

function search(arr, item) {


for(index in arr){
if(arr[index] === item){
return Number(index);
}
}
return -1;
}

Area of a triangle

function triArea(base, height) {


return (base * height) / 2;
}

You might also like