├── 01_reverse_string ├── test.js └── index.js ├── 04_max_chars ├── test.js └── index.js ├── 08_capitalize ├── test.js └── index.js ├── package.json ├── 03_integer_reverse ├── test.js └── index.js ├── 07_anagrams ├── test.js └── index.js ├── 02_palindrome ├── index.js └── test.js ├── 05_fizzbuzz ├── index.js └── test.js ├── 09_steps ├── test.js └── index.js ├── 06_array_chunking ├── test.js └── index.js ├── 10_pyramid ├── test.js └── index.js └── README.md /01_reverse_string/test.js: -------------------------------------------------------------------------------- 1 | const reverse = require('./index'); 2 | 3 | test('Reverse function exists', () => { 4 | expect(reverse).toBeDefined(); 5 | }); 6 | 7 | test('Reverse reverses a string', () => { 8 | expect(reverse('abcd')).toEqual('dcba'); 9 | }); 10 | 11 | test('Reverse reverses a string', () => { 12 | expect(reverse(' abcd')).toEqual('dcba '); 13 | }); 14 | -------------------------------------------------------------------------------- /04_max_chars/test.js: -------------------------------------------------------------------------------- 1 | const maxChar = require('./index'); 2 | 3 | test('maxChar function exists', () => { 4 | expect(typeof maxChar).toEqual('function'); 5 | }); 6 | 7 | test('Finds the most frequently used char', () => { 8 | expect(maxChar('a')).toEqual('a'); 9 | expect(maxChar('abcdefghijklmnaaaaa')).toEqual('a'); 10 | }); 11 | 12 | test('Works with numbers in the string', () => { 13 | expect(maxChar('ab1c1d1e1f1g1')).toEqual('1'); 14 | }); 15 | -------------------------------------------------------------------------------- /08_capitalize/test.js: -------------------------------------------------------------------------------- 1 | const capitalize = require('./index'); 2 | 3 | test('Capitalize is a function', () => { 4 | expect(typeof capitalize).toEqual('function'); 5 | }); 6 | 7 | test('capitalizes the first letter of every word in a sentence', () => { 8 | expect(capitalize('hi there, how is it going?')).toEqual( 9 | 'Hi There, How Is It Going?' 10 | ); 11 | }); 12 | 13 | test('capitalizes the first letter', () => { 14 | expect(capitalize('i love breakfast at bill miller bbq')).toEqual( 15 | 'I Love Breakfast At Bill Miller Bbq' 16 | ); 17 | }); 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "coding-interview-algorithms-and-data-structure", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/somekindofwallflower/coding-interview-algorithms-and-data-structure.git" 12 | }, 13 | "author": "somekindofwallflower", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/somekindofwallflower/coding-interview-algorithms-and-data-structure/issues" 17 | }, 18 | "homepage": "https://github.com/somekindofwallflower/coding-interview-algorithms-and-data-structure#readme", 19 | "dependencies": { 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /03_integer_reverse/test.js: -------------------------------------------------------------------------------- 1 | const reverseInt = require('./index'); 2 | 3 | test('ReverseInt function exists', () => { 4 | expect(reverseInt).toBeDefined(); 5 | }); 6 | 7 | test('ReverseInt handles 0 as an input', () => { 8 | expect(reverseInt(0)).toEqual(0); 9 | }); 10 | 11 | test('ReverseInt flips a positive number', () => { 12 | expect(reverseInt(5)).toEqual(5); 13 | expect(reverseInt(15)).toEqual(51); 14 | expect(reverseInt(90)).toEqual(9); 15 | expect(reverseInt(2359)).toEqual(9532); 16 | }); 17 | 18 | test('ReverseInt flips a negative number', () => { 19 | expect(reverseInt(-5)).toEqual(-5); 20 | expect(reverseInt(-15)).toEqual(-51); 21 | expect(reverseInt(-90)).toEqual(-9); 22 | expect(reverseInt(-2359)).toEqual(-9532); 23 | }); 24 | -------------------------------------------------------------------------------- /01_reverse_string/index.js: -------------------------------------------------------------------------------- 1 | // --- Directions 2 | // Given a string, return a new string with the reversed 3 | // order of characters 4 | // --- Examples 5 | // reverse('apple') === 'leppa' 6 | // reverse('hello') === 'olleh' 7 | // reverse('Greetings!') === '!sgniteerG' 8 | 9 | 10 | // Solution 1 11 | // function reverse(str) { 12 | // return str.split('').reverse().join(''); 13 | // } 14 | 15 | 16 | // // Solution 2 17 | // function reverse(str) { 18 | // let reversed = ""; 19 | // for(let character of str) { 20 | // reversed = character + reversed; 21 | // } 22 | // return reversed; 23 | // } 24 | 25 | // Solution 3 26 | function reverse(str) { 27 | return str.split("").reduce((rev, char) => char + rev, "") 28 | } 29 | 30 | 31 | module.exports = reverse; 32 | -------------------------------------------------------------------------------- /03_integer_reverse/index.js: -------------------------------------------------------------------------------- 1 | // --- Directions 2 | // Given an integer, return an integer that is the reverse 3 | // ordering of numbers. 4 | // --- Examples 5 | // reverseInt(15) === 51 6 | // reverseInt(981) === 189 7 | // reverseInt(500) === 5 8 | // reverseInt(-15) === -51 9 | // reverseInt(-90) === -9 10 | 11 | // Tips to solve the problem 12 | // - Convert number to string by using toString function 13 | // - Check if it is a sign number or not by using Math.sign() function. 14 | // - Convert string to number by using parseInt method 15 | 16 | function reverseInt(n) { 17 | const reversed = n 18 | .toString() 19 | .split('') 20 | .reverse() 21 | .join(''); 22 | return parseInt(reversed) * Math.sign(n); 23 | } 24 | 25 | 26 | module.exports = reverseInt; 27 | -------------------------------------------------------------------------------- /04_max_chars/index.js: -------------------------------------------------------------------------------- 1 | // --- Directions 2 | // Given a string, return the character that is most 3 | // commonly used in the string. 4 | // --- Examples 5 | // maxChar("abcccccccd") === "c" 6 | // maxChar("apple 1231111") === "1" 7 | 8 | // Tips: Convert string to object where the keys are the character and values is the number of times the character has been found 9 | 10 | function maxChar(str) { 11 | // Generate object from string 12 | const charMap = {}; 13 | let max = 0; 14 | let maxChar = ""; 15 | for(let char of str) { 16 | charMap[char] = charMap[char] + 1 || 1; 17 | } 18 | for (let char in charMap) { 19 | if(charMap[char] > max) { 20 | max = charMap[char]; 21 | maxChar = char; 22 | } 23 | } 24 | return maxChar; 25 | } 26 | 27 | module.exports = maxChar; 28 | -------------------------------------------------------------------------------- /07_anagrams/test.js: -------------------------------------------------------------------------------- 1 | const anagrams = require('./index.js'); 2 | 3 | test('anagrams function exists', () => { 4 | expect(typeof anagrams).toEqual('function'); 5 | }); 6 | 7 | test('"hello" is an anagram of "llohe"', () => { 8 | expect(anagrams('hello', 'llohe')).toBeTruthy(); 9 | }); 10 | 11 | test('"Whoa! Hi!" is an anagram of "Hi! Whoa!"', () => { 12 | expect(anagrams('Whoa! Hi!', 'Hi! Whoa!')).toBeTruthy(); 13 | }); 14 | 15 | test('"One One" is not an anagram of "Two two two"', () => { 16 | expect(anagrams('One One', 'Two two two')).toBeFalsy(); 17 | }); 18 | 19 | test('"One one" is not an anagram of "One one c"', () => { 20 | expect(anagrams('One one', 'One one c')).toBeFalsy(); 21 | }); 22 | 23 | test('"A tree, a life, a bench" is not an anagram of "A tree, a fence, a yard"', () => { 24 | expect( 25 | anagrams('A tree, a life, a bench', 'A tree, a fence, a yard') 26 | ).toBeFalsy(); 27 | }); 28 | -------------------------------------------------------------------------------- /02_palindrome/index.js: -------------------------------------------------------------------------------- 1 | // --- Directions 2 | // Given a string, return true if the string is a palindrome 3 | // or false if it is not. Palindromes are strings that 4 | // form the same word if it is reversed. *Do* include spaces 5 | // and punctuation in determining if the string is a palindrome. 6 | // --- Examples: 7 | // palindrome("abba") === true 8 | // palindrome("abcdefg") === false 9 | 10 | 11 | // Solution 1 12 | // Reverse the string and store the result in an variable than compare string to the reversed value 13 | // function palindrome(str) { 14 | // // const reverseStr = str.split("").reduce((rev, char) => char + rev, ""); 15 | // const reverseStr = str.split("").reverse().join(""); 16 | // return reverseStr === str; 17 | // } 18 | 19 | // Solution 2 20 | function palindrome(str) { 21 | return str.split('').every((char, i, arr) => char === arr[arr.length - i - 1]) 22 | } 23 | module.exports = palindrome; 24 | -------------------------------------------------------------------------------- /02_palindrome/test.js: -------------------------------------------------------------------------------- 1 | const palindrome = require('./index'); 2 | 3 | test("palindrome function is defined", () => { 4 | expect(typeof palindrome).toEqual("function"); 5 | }) 6 | 7 | 8 | test("'aba' is palindrome", () => { 9 | expect(palindrome("aba")).toBeTruthy(); 10 | }) 11 | 12 | test("' aba' is not palindrome", () => { 13 | expect(palindrome(" aba")).toBeFalsy(); 14 | }) 15 | 16 | test("'aba ' is not palindrome", () => { 17 | expect(palindrome("aba ")).toBeFalsy(); 18 | }) 19 | 20 | test('"greetings" is not a palindrome', () => { 21 | expect(palindrome('greetings')).toBeFalsy(); 22 | }); 23 | 24 | test('"1000000001" a palindrome', () => { 25 | expect(palindrome('1000000001')).toBeTruthy(); 26 | }); 27 | 28 | test('"Fish hsif" is not a palindrome', () => { 29 | expect(palindrome('Fish hsif')).toBeFalsy(); 30 | }); 31 | 32 | test('"pennep" a palindrome', () => { 33 | expect(palindrome('pennep')).toBeTruthy(); 34 | }); 35 | -------------------------------------------------------------------------------- /05_fizzbuzz/index.js: -------------------------------------------------------------------------------- 1 | // --- Directions 2 | // Write a program that console logs the numbers 3 | // from 1 to n. But for multiples of three print 4 | // “fizz” instead of the number and for the multiples 5 | // of five print “buzz”. For numbers which are multiples 6 | // of both three and five print “fizzbuzz”. 7 | // --- Example 8 | // fizzBuzz(5); 9 | // 1 10 | // 2 11 | // fizz 12 | // 4 13 | // buzz 14 | 15 | function fizzBuzz(n) { 16 | for (let i = 1; i <= n; i++) { 17 | // Is the number multiple of 3 and 5? 18 | if(i % 3 === 0 && i % 5 === 0) { 19 | console.log('fizzbuzz'); 20 | // Is the number multiple of 3? 21 | } else if (i % 3 === 0) { 22 | console.log('fizz'); 23 | // Is the number multiple of 5? 24 | } else if (i % 5 === 0) { 25 | console.log('buzz'); 26 | } else { 27 | console.log(i); 28 | } 29 | } 30 | } 31 | 32 | module.exports = fizzBuzz; 33 | -------------------------------------------------------------------------------- /09_steps/test.js: -------------------------------------------------------------------------------- 1 | const steps = require('./index'); 2 | 3 | beforeEach(() => { 4 | jest.spyOn(console, 'log'); 5 | }); 6 | 7 | afterEach(() => { 8 | console.log.mockRestore(); 9 | }); 10 | 11 | test('steps is a function', () => { 12 | expect(typeof steps).toEqual('function'); 13 | }); 14 | 15 | test('steps called with n = 1', () => { 16 | steps(1); 17 | expect(console.log.mock.calls[0][0]).toEqual('#'); 18 | expect(console.log.mock.calls.length).toEqual(1); 19 | }); 20 | 21 | test('steps called with n = 2', () => { 22 | steps(2); 23 | expect(console.log.mock.calls[0][0]).toEqual('# '); 24 | expect(console.log.mock.calls[1][0]).toEqual('##'); 25 | expect(console.log.mock.calls.length).toEqual(2); 26 | }); 27 | 28 | test('steps called with n = 3', () => { 29 | steps(3); 30 | expect(console.log.mock.calls[0][0]).toEqual('# '); 31 | expect(console.log.mock.calls[1][0]).toEqual('## '); 32 | expect(console.log.mock.calls[2][0]).toEqual('###'); 33 | expect(console.log.mock.calls.length).toEqual(3); 34 | }); 35 | -------------------------------------------------------------------------------- /06_array_chunking/test.js: -------------------------------------------------------------------------------- 1 | const chunk = require('./index'); 2 | 3 | test('function chunk exists', () => { 4 | expect(typeof chunk).toEqual('function'); 5 | }); 6 | 7 | test('chunk divides an array of 10 elements with chunk size 2', () => { 8 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 9 | const chunked = chunk(arr, 2); 10 | 11 | expect(chunked).toEqual([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]); 12 | }); 13 | 14 | test('chunk divides an array of 3 elements with chunk size 1', () => { 15 | const arr = [1, 2, 3]; 16 | const chunked = chunk(arr, 1); 17 | 18 | expect(chunked).toEqual([[1], [2], [3]]); 19 | }); 20 | 21 | test('chunk divides an array of 5 elements with chunk size 3', () => { 22 | const arr = [1, 2, 3, 4, 5]; 23 | const chunked = chunk(arr, 3); 24 | 25 | expect(chunked).toEqual([[1, 2, 3], [4, 5]]); 26 | }); 27 | 28 | test('chunk divides an array of 13 elements with chunk size 5', () => { 29 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; 30 | const chunked = chunk(arr, 5); 31 | 32 | expect(chunked).toEqual([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13]]); 33 | }); 34 | -------------------------------------------------------------------------------- /08_capitalize/index.js: -------------------------------------------------------------------------------- 1 | // --- Directions 2 | // Write a function that accepts a string. The function should 3 | // capitalize the first letter of each word in the string then 4 | // return the capitalized string. 5 | // --- Examples 6 | // capitalize('a short sentence') --> 'A Short Sentence' 7 | // capitalize('a lazy fox') --> 'A Lazy Fox' 8 | // capitalize('look, it is working!') --> 'Look, It Is Working!' 9 | 10 | // Solution 1 11 | // function capitalize(str) { 12 | // let result = []; 13 | // for(let word of str.split(" ")) { 14 | // result.push(`${word[0].toUpperCase()}${word.slice(1)}`) 15 | // } 16 | // return result.join(" ") 17 | // } 18 | 19 | // Solution 2 20 | // Create a string with the first element of the string capitalized 21 | // For each character of the string if the character on the left is space capitalize and add to the result else add it to the result 22 | function capitalize(str) { 23 | let result = str[0].toUpperCase(); 24 | 25 | for (let i = 1; i < str.length; i++) { 26 | if (str[i - 1] === ' ') { 27 | result += str[i].toUpperCase(); 28 | } else { 29 | result += str[i]; 30 | } 31 | } 32 | 33 | return result; 34 | } 35 | 36 | module.exports = capitalize; 37 | -------------------------------------------------------------------------------- /10_pyramid/test.js: -------------------------------------------------------------------------------- 1 | const pyramid = require('./index'); 2 | 3 | beforeEach(() => { 4 | jest.spyOn(console, 'log'); 5 | }); 6 | 7 | afterEach(() => { 8 | console.log.mockRestore(); 9 | }); 10 | 11 | test('pyramid is a function', () => { 12 | expect(typeof pyramid).toEqual('function'); 13 | }); 14 | 15 | test('prints a pryamid for n = 2', () => { 16 | pyramid(2); 17 | expect(console.log.mock.calls[0][0]).toEqual(' # '); 18 | expect(console.log.mock.calls[1][0]).toEqual('###'); 19 | expect(console.log.mock.calls.length).toEqual(2); 20 | }); 21 | 22 | test('prints a pryamid for n = 3', () => { 23 | pyramid(3); 24 | expect(console.log.mock.calls[0][0]).toEqual(' # '); 25 | expect(console.log.mock.calls[1][0]).toEqual(' ### '); 26 | expect(console.log.mock.calls[2][0]).toEqual('#####'); 27 | expect(console.log.mock.calls.length).toEqual(3); 28 | }); 29 | 30 | test('prints a pryamid for n = 4', () => { 31 | pyramid(4); 32 | expect(console.log.mock.calls[0][0]).toEqual(' # '); 33 | expect(console.log.mock.calls[1][0]).toEqual(' ### '); 34 | expect(console.log.mock.calls[2][0]).toEqual(' ##### '); 35 | expect(console.log.mock.calls[3][0]).toEqual('#######'); 36 | expect(console.log.mock.calls.length).toEqual(4); 37 | }); 38 | -------------------------------------------------------------------------------- /05_fizzbuzz/test.js: -------------------------------------------------------------------------------- 1 | const fizzBuzz = require('./index'); 2 | 3 | test('fizzBuzz function is defined', () => { 4 | expect(fizzBuzz).toBeDefined(); 5 | }); 6 | 7 | test('Calling fizzbuzz with `5` prints out 5 statements', () => { 8 | fizzBuzz(5); 9 | 10 | expect(console.log.mock.calls.length).toEqual(5); 11 | }); 12 | 13 | test('Calling fizzbuzz with 15 prints out the correct values', () => { 14 | fizzBuzz(15); 15 | 16 | expect(console.log.mock.calls[0][0]).toEqual(1); 17 | expect(console.log.mock.calls[1][0]).toEqual(2); 18 | expect(console.log.mock.calls[2][0]).toEqual('fizz'); 19 | expect(console.log.mock.calls[3][0]).toEqual(4); 20 | expect(console.log.mock.calls[4][0]).toEqual('buzz'); 21 | expect(console.log.mock.calls[5][0]).toEqual('fizz'); 22 | expect(console.log.mock.calls[6][0]).toEqual(7); 23 | expect(console.log.mock.calls[7][0]).toEqual(8); 24 | expect(console.log.mock.calls[8][0]).toEqual('fizz'); 25 | expect(console.log.mock.calls[9][0]).toEqual('buzz'); 26 | expect(console.log.mock.calls[10][0]).toEqual(11); 27 | expect(console.log.mock.calls[11][0]).toEqual('fizz'); 28 | expect(console.log.mock.calls[12][0]).toEqual(13); 29 | expect(console.log.mock.calls[13][0]).toEqual(14); 30 | expect(console.log.mock.calls[14][0]).toEqual('fizzbuzz'); 31 | }); 32 | 33 | beforeEach(() => { 34 | jest.spyOn(console, 'log'); 35 | }); 36 | 37 | afterEach(() => { 38 | console.log.mockRestore(); 39 | }); 40 | -------------------------------------------------------------------------------- /10_pyramid/index.js: -------------------------------------------------------------------------------- 1 | // --- Directions 2 | // Write a function that accepts a positive number N. 3 | // The function should console log a pyramid shape 4 | // with N levels using the # character. Make sure the 5 | // pyramid has spaces on both the left *and* right hand sides 6 | // --- Examples 7 | // pyramid(1) 8 | // '#' 9 | // pyramid(2) 10 | // ' # ' 11 | // '###' 12 | // pyramid(3) 13 | // ' # ' 14 | // ' ### ' 15 | // '#####' 16 | 17 | // Solution 1 18 | // function pyramid(n) { 19 | // const midPoint = Math.floor((2 * n - 1)/2) 20 | // for(let row = 0; row < n; row++) { 21 | // let level = ''; 22 | // for(let column = 0; column < 2*n - 1; column++) { 23 | // if(midPoint - row <= column && midPoint + row >=column) { 24 | // level += '#' 25 | // } else { 26 | // level += ' ' 27 | // } 28 | // } 29 | // console.log(level); 30 | // } 31 | // } 32 | 33 | // Solution 2 34 | function pyramid(n, row = 0, str = "") { 35 | if (n === row) { 36 | return; 37 | } 38 | const midPoint = Math.floor((2 * n - 1) / 2) 39 | if (str.length === 2 * n - 1) { 40 | console.log(str); 41 | return pyramid(n, row + 1) 42 | } else { 43 | if (midPoint - row <= str.length && midPoint + row >= str.length) { 44 | str += '#' 45 | } else { 46 | str += ' ' 47 | } 48 | return pyramid(n, row, str) 49 | } 50 | } 51 | 52 | module.exports = pyramid; 53 | -------------------------------------------------------------------------------- /07_anagrams/index.js: -------------------------------------------------------------------------------- 1 | // --- Directions 2 | // Check to see if two provided strings are anagrams of each other. 3 | // One string is an anagram of another if it uses the same characters 4 | // in the same quantity. Only consider characters, not spaces 5 | // or punctuation. Consider capital letters to be the same as lower case 6 | // --- Examples 7 | // anagrams('rail safety', 'fairy tales') --> True 8 | // anagrams('RAIL! SAFETY!', 'fairy tales') --> True 9 | // anagrams('Hi there', 'Bye there') --> False 10 | 11 | // Solution 1 12 | // function anagrams(stringA, stringB) { 13 | // const aCharMap = buildCharMap(stringA); 14 | // const bCharMap = buildCharMap(stringB); 15 | // 16 | // if(Object.keys(aCharMap).length !== Object.keys(bCharMap).length) { 17 | // return false; 18 | // } 19 | // for (let char in aCharMap) { 20 | // if(aCharMap[char] !== bCharMap[char]) { 21 | // return false 22 | // } 23 | // } 24 | // return true; 25 | // } 26 | // 27 | // function buildCharMap(str) { 28 | // const charMap = {}; 29 | // for (let char of str.replace(/[^\w]/g, '').toLowerCase()) { 30 | // charMap[char] = charMap[char] + 1 || 1; 31 | // } 32 | // return charMap; 33 | // } 34 | 35 | 36 | // Solution 2 37 | function anagrams(stringA, stringB) { 38 | return cleanStr(stringA) === cleanStr(stringB); 39 | } 40 | 41 | function cleanStr(str) { 42 | return str.replace(/[^\w]/g, '') 43 | .toLowerCase() 44 | .split("") 45 | .sort() 46 | .join(""); 47 | } 48 | module.exports = anagrams; 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # The Coding Interview: Algorithms + Data Structures 2 | ### Ace your next Javascript coding interview by mastering data structures and algorithms. 3 | 4 | - [Problem 1: String Reversal](https://github.com/somekindofwallflower/coding-interview-algorithms-and-data-structure/tree/master/01_reverse_string) 5 | - [Problem 2: Palindromes](https://github.com/somekindofwallflower/coding-interview-algorithms-and-data-structure/tree/master/02_palindrome) 6 | - [Problem 3: Integer Reversal](https://github.com/somekindofwallflower/coding-interview-algorithms-and-data-structure/tree/master/03_integer_reverse) 7 | - [Problem 4: Max Char](https://github.com/somekindofwallflower/coding-interview-algorithms-and-data-structure/tree/master/04_max_chars) 8 | - [Problem 5: FizzBuzz](https://github.com/somekindofwallflower/coding-interview-algorithms-and-data-structure/tree/master/05_fizzbuzz) 9 | - [Problem 6: Array Chunking](https://github.com/somekindofwallflower/coding-interview-algorithms-and-data-structure/tree/master/06_array_chunking) 10 | - [Problem 7: Anagrams](https://github.com/somekindofwallflower/coding-interview-algorithms-and-data-structure/tree/master/07_anagrams) 11 | - [Problem 8: Capitalize](https://github.com/somekindofwallflower/coding-interview-algorithms-and-data-structure/tree/master/08_capitalize) 12 | - [Problem 9: Steps](https://github.com/somekindofwallflower/coding-interview-algorithms-and-data-structure/tree/master/09_steps) 13 | - [Problem 10: Tow sides pyramid](https://github.com/somekindofwallflower/coding-interview-algorithms-and-data-structure/tree/master/10_pyramid) 14 | -------------------------------------------------------------------------------- /09_steps/index.js: -------------------------------------------------------------------------------- 1 | // --- Directions 2 | // Write a function that accepts a positive number N. 3 | // The function should console log a step shape 4 | // with N levels using the # character. Make sure the 5 | // step has spaces on the right hand side! 6 | // --- Examples 7 | // steps(2) 8 | // '# ' 9 | // '##' 10 | // steps(3) 11 | // '# ' 12 | // '## ' 13 | // '###' 14 | // steps(4) 15 | // '# ' 16 | // '## ' 17 | // '### ' 18 | // '####' 19 | 20 | // Solution 1 21 | // function steps(n) { 22 | // if(n === 1) { 23 | // console.log("#"); 24 | // } else { 25 | // for(let s = 1; s <= n; s++) { 26 | // console.log('#'.repeat(s) + ' '.repeat(n - s)) 27 | // 28 | // } 29 | // } 30 | // } 31 | 32 | // Solution 2 33 | 34 | // function steps(n) { 35 | // for(let row = 0; row < n; row++) { 36 | // let stair = ""; 37 | // 38 | // for (let column = 0; column < n; column ++) { 39 | // if(column <= row) { 40 | // stair += '#'; 41 | // } else { 42 | // stair += ' '; 43 | // } 44 | // } 45 | // console.log(stair); 46 | // } 47 | // } 48 | 49 | function steps(n, row = 0, stair= '') { 50 | if (n === row) { 51 | return; 52 | } 53 | if(n === stair.length) { 54 | console.log(stair); 55 | return steps(n, row + 1); 56 | } else { 57 | if(stair.length <= row) { 58 | stair += '#' 59 | } else { 60 | stair += ' ' 61 | } 62 | return steps(n, row, stair); 63 | } 64 | } 65 | 66 | module.exports = steps; 67 | -------------------------------------------------------------------------------- /06_array_chunking/index.js: -------------------------------------------------------------------------------- 1 | // --- Directions 2 | // Given an array and chunk size, divide the array into many subarrays 3 | // where each subarray is of length size 4 | // --- Examples 5 | // chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]] 6 | // chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]] 7 | // chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, 8]] 8 | // chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]] 9 | // chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]] 10 | 11 | // Steps 12 | // Create empty array to hold chunks called 'chunked' 13 | // Loop through each element in the unchanked array 14 | // Retrieve the last element in the chunked 15 | // If element does not exist or the element length is equal to chunk size push e new chunk to the chunked array with the element 16 | // Else add the element into the chunk 17 | 18 | 19 | // Solution 1 20 | // function chunk(array, size) { 21 | // let chunked = []; 22 | // for (let element of array) { 23 | // const last = chunked[chunked.length - 1] 24 | // if(!last || last.length === size) { 25 | // chunked.push([element]) 26 | // } else { 27 | // last.push(element); 28 | // } 29 | // } 30 | // return chunked; 31 | // } 32 | 33 | 34 | // Steps 35 | // Create an empty chunk array 36 | // Create 'index' starts at 0 37 | // While index is less than array.length 38 | // Push a slice of length size from array into chunked 39 | // Add size to index 40 | 41 | // Solution 2 42 | function chunk(array, size) { 43 | let chunked = []; 44 | let index = 0; 45 | for (let el of array) { 46 | if(index < array.length) { 47 | chunked.push(array.slice(index, index + size)) 48 | index = index + size; 49 | } 50 | } 51 | return chunked; 52 | } 53 | 54 | 55 | module.exports = chunk; 56 | --------------------------------------------------------------------------------