├── .vscode └── settings.json ├── ReadMe.md ├── package.json ├── find_Longest_Word.js ├── First_Reverse.js ├── .gitignore └── meanMode.js /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "eslint.enable": false 3 | } -------------------------------------------------------------------------------- /ReadMe.md: -------------------------------------------------------------------------------- 1 | Cracking the Coding Interview Exercise questions implemented in JavaScript 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "coderbyte", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "keywords": [ 7 | "interview", 8 | "coding", 9 | "algorithms" 10 | ], 11 | "scripts": { 12 | "test": "./node_modules/.bin/mocha --reporter spec" 13 | }, 14 | "author": "Rohan Paul", 15 | "license": "ISC", 16 | "devDependencies": { 17 | "babel-core": "^6.26.0", 18 | "chai": "^4.1.2", 19 | "mocha": "^4.0.1", 20 | "tape": "^4.8.0" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /find_Longest_Word.js: -------------------------------------------------------------------------------- 1 | function LongestWord(sen) { 2 | 3 | // First convert the string into an array with only the words. 4 | // Then compare each words.length (after removing special characters from word) and sort these lengths in descending order. 5 | // return the first element from that sorted array. 6 | 7 | var sentence = sen.split(" ") 8 | .sort(function(a,b){ 9 | return b.replace(/[^a-zA-Z]/g, "").length - a.replace(/[^a-zA-Z]/g, "").length; 10 | }); 11 | return sentence.shift(); 12 | } -------------------------------------------------------------------------------- /First_Reverse.js: -------------------------------------------------------------------------------- 1 | /*Have the function FirstReverse(str) take the str parameter being passed and return the string in reversed order. For example: if the input string is "Hello World and Coders" then your program should return the string sredoC dna dlroW olleH. 2 | 3 | Use the Parameter Testing feature in the box below to test your code with different arguments. 4 | */ 5 | 6 | function FirstReverse(str) { 7 | var reverse = ''; 8 | for(var i = str.length - 1; i >= 0; i--) 9 | reverse += str[i]; 10 | return reverse; 11 | return str; 12 | 13 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore docs files 2 | _gh_pages 3 | .ruby-version 4 | 5 | # Numerous always-ignore extensions 6 | *.diff 7 | *.err 8 | *.orig 9 | *.log 10 | *.rej 11 | *.swo 12 | *.swp 13 | *.zip 14 | *.vi 15 | *~ 16 | *.~lock* 17 | .~lock* 18 | 19 | # OS or Editor folders 20 | .DS_Store 21 | ._* 22 | Thumbs.db 23 | .cache 24 | .project 25 | .settings 26 | .tmproj 27 | *.esproj 28 | nbproject 29 | *.sublime-project 30 | *.sublime-workspace 31 | .idea 32 | 33 | # Komodo 34 | *.komodoproject 35 | .komodotools 36 | 37 | # grunt-html-validation 38 | validation-status.json 39 | validation-report.json 40 | 41 | # Folders to ignore 42 | node_modules 43 | 44 | # Ignore all logfiles and tempfiles. 45 | !/log/.keep 46 | /tmp 47 | /.gems 48 | 49 | testing-code-3.js 50 | testing-scribling-code.js 51 | testing-code-1.js 52 | Cracking-The-Coding-Interview-Note-Gitignore-IT.odt 53 | 54 | 55 | #ignore file name ending in "-bkp.js" OR "-bkp.ts" OR "-bkp.py" OR "-test.js" OR "-test.ts" OR "-test.py" in its name. So I will have to put "-test.js" at all files that is just for my development-time random testing code . 56 | **/*-bkp.js 57 | **/*-bkp.ts 58 | **/*-bkp.py 59 | **/*-test.js 60 | **/*-test.ts 61 | **/*-test.py -------------------------------------------------------------------------------- /meanMode.js: -------------------------------------------------------------------------------- 1 | /* https://coderbyte.com/question/mean-mode 2 | 3 | * Have the function MeanMode(arr) take the array of numbers stored in arr and * 4 | * return 1 if the mode equals the mean, 0 if they don't equal each other * 5 | * (ie. [5, 3, 3, 3, 1] should return 1 because the mode (3) equals the mean (3)). * 6 | * The array will not be empty, will only contain positive integers, and will not * 7 | * contain more than one mode. * 8 | * * 9 | * SOLUTION * 10 | * Since it is possible that I will want a function that will calculate the mean or * 11 | * mode in the future, I decided to create separate functions for each. The mean is * 12 | * calculated by the average of all values in the array. The mode is the number that * 13 | * exists the most in the array. My solution is to call my two functions and then * 14 | * compare to see if they are equal and if so return 1 else return 0. * 15 | * * 16 | * Steps for solution * 17 | * 1) Create separate functions for getMean and getMode * 18 | * 2) Compare the values returned from the two functions * 19 | * 3) If values are equal return 1 else return 0 20 | */ 21 | 22 | mean = arr => (arr.reduce((a, b) => a + b))/(arr.length); 23 | 24 | // mode is just the problem of finding the element with greatest no of occurrence 25 | mode = arr => { 26 | 27 | let obj = {}, max = 1, mode; 28 | 29 | for (let i of arr) { 30 | obj[i] = obj[i] || 0; 31 | obj[i]++ 32 | } 33 | 34 | for (let i in obj) { 35 | if (obj.hasOwnProperty(i)) { 36 | if ( obj[i] > max ) { 37 | max = obj[i] 38 | mode = i; 39 | } 40 | } 41 | } 42 | return mode; 43 | } 44 | 45 | meanMode = arr => mean(arr) == mode(arr) 46 | 47 | let myArr = [5, 3, 3, 3, 1] 48 | 49 | console.log(mean(myArr)) 50 | 51 | console.log(mode(myArr)) 52 | 53 | console.log(meanMode(myArr)) --------------------------------------------------------------------------------