Last active
November 17, 2019 12:45
-
-
Save daredrum/357115aafa502c84a86f1421ed57c45a to your computer and use it in GitHub Desktop.
Javascript Interview Tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1. add(2,4)(5,3)(2)(3,1); // 20 | |
| function add(...argA) { | |
| const getSum = (arg) => arg.reduce((s, n) => s += n, 0); | |
| let sum = getSum(argA); | |
| const f = (...argB) => { | |
| sum += getSum(argB); | |
| return f; | |
| } | |
| f.valueOf = () => sum; | |
| return f; | |
| } | |
| 2. Fibonacci sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... | |
| Recursion: | |
| function fib(num) { | |
| return num > 1 ? fib(num - 1) + fib(num - 2) : 1; | |
| } | |
| Loop: | |
| function fibn(num) { | |
| let a = 0, b = 1, temp; | |
| while (num > 0) { | |
| temp = b; | |
| b = a + b; | |
| a = temp; | |
| num--; | |
| } | |
| return b; | |
| } | |
| 3. Flatten function | |
| function flatten() { | |
| return arr.reduce((s,item) => { | |
| const flatItem = (item insteanceOf Array) ? flatten(item) : item; | |
| return s.concat(flatItem); | |
| }, []); | |
| } | |
| 4. Calculate avarage age of males | |
| const survives = { | |
| 'Max': { age: 18, isMale: true }, | |
| 'Den': { age: 22, isMale: true }, | |
| 'Oli': { age: 20, isMale: false }, | |
| }; | |
| Object.keys(survives) | |
| .map(key => survives[key]) | |
| .filter(({ isMale }) => isMale) | |
| .reduce((sum, { age }, index, { length }) => sum + age / length, 0); | |
| 5. Binary search | |
| Recursion: | |
| function binary_search(arr, item, start = 0, end = arr.length - 1) { | |
| const middle = Math.floor((end + start) / 2); | |
| const middleItem = arr[middle]; | |
| if (middleItem === item) { | |
| return middle; | |
| } | |
| if (start === end) { | |
| return null; | |
| } | |
| if (middleItem < item) { | |
| return binary_search(arr, item, middle + 1, end); | |
| } else { | |
| return binary_search(arr, item, start, middle - 1, end); | |
| } | |
| } | |
| Loop: | |
| function binary_search(arr, item) { | |
| let start = 0; | |
| let end = arr.length - 1; | |
| while (start <= end) { | |
| const middle = Math.floor((end + start) / 2); | |
| const middleItem = arr[middle]; | |
| if (middleItem === item) { | |
| return middle; | |
| } | |
| if (middleItem < item) { | |
| start = middle + 1; | |
| } else { | |
| end = middle - 1; | |
| } | |
| } | |
| return null; | |
| } | |
| binary_search([1,2,3,4,5], 3) // 2 | |
| binary_search([1,2,3,4,5], 9) // null | |
| 6. Find subset with sum of all items equals to "n" | |
| Сomplexity O(2n) -> O(n): | |
| function findSubsets(arr, n) { | |
| let i = 0; | |
| let j = 0; | |
| const result = []; | |
| while (i < arr.length) { | |
| const sum = arr.slice(i, j + 1).reduce((s, v) => (s += v), 0); | |
| if (sum === n) { | |
| result.push([i, j]); | |
| } | |
| if (sum < n && j + 1 !== arr.length) { | |
| j++; | |
| } else { | |
| i++; | |
| } | |
| } | |
| return result; | |
| } | |
| findSubsets([4, 2, 1, 3, 7, 5, 1, 9], 6); // [[0, 1], [1, 3], [5, 6]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment