Last active
July 6, 2017 10:43
-
-
Save kikill95/89440b01a1680dfd969f3ba03c91b8f8 to your computer and use it in GitHub Desktop.
Implement the following functions
This file contains 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. // isPrime - Returns true or false, indicating whether the given number is prime. | |
isPrime(0) // false | |
isPrime(1) // false | |
isPrime(17) // true | |
isPrime(10000000000000) // false | |
2. // factorial - Returns a number that is the factorial of the given number. | |
factorial(0) // 1 | |
factorial(1) // 1 | |
factorial(6) // 720 | |
3. // fib - Returns the nth Fibonacci number. | |
fib(0) // 0 | |
fib(1) // 1 | |
fib(10) // 55 | |
fib(20) // 6765 | |
4. // isSorted - Returns true or false, indicating whether the given array of numbers is sorted. | |
isSorted([]) // true | |
isSorted([-Infinity, -5, 0, 3, 9]) // true | |
isSorted([3, 9, -3, 10]) // false | |
5. // reverse - Reverses the given string (yes, using the built in reverse function is cheating). | |
reverse('') // '' | |
reverse('abcdef') // 'fedcba' | |
6. //indexOf - Implement the indexOf function for arrays. | |
indexOf([1, 2, 3], 1) // 0 | |
indexOf([1, 2, 3], 4) // -1 | |
7. // isPalindrome - Return true or false indicating whether the given string is a plaindrone (case and space insensitive). | |
isPalindrome('') // true | |
isPalindrome('abcdcba') // true | |
isPalindrome('abcd') // false | |
isPalindrome('A man a plan a canal Panama') // true | |
8. // missing - Takes an unsorted array of unique numbers (ie. no repeats) from 1 through some number n, and returns the missing number in the sequence (there are either no missing numbers, or exactly one missing number). Can you do it in O(N) time? Hint: There’s a clever formula you can use. | |
missing([]) // undefined | |
missing([1, 4, 3]) // 2 | |
missing([2, 3, 4]) // 1 | |
missing([5, 1, 4, 2]) // 3 | |
missing([1, 2, 3, 4]) // undefined | |
9. // isBalanced - Takes a string and returns true or false indicating whether its curly braces are balanced. | |
isBalanced('}{') // false | |
isBalanced('{{}') // false | |
isBalanced('{}{}') // false | |
isBalanced('foo { bar { baz } boo }') // true | |
isBalanced('foo { bar { baz }') // false | |
isBalanced('foo { bar } }') // false |
This file contains 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. // permute - Return an array of strings, containing every permutation of the given string. | |
permute('') // [] | |
permute('abc') // ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'] | |
2. // seq - Resolve an array of promises in sequence (as opposed to Promise.all, which does it in parallel). | |
let a = Promise.resolve('a') | |
let b = Promise.resolve('b') | |
let c = Promise.resolve('c') | |
await seq([a, b, c]) // ['a', 'b', 'c'] | |
await seq([a, c, b]) // ['a', 'c', 'b'] |
This file contains 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. // isBalanced2 - Like the isBalanced function you implemented above, but supports 3 types of braces: curly {}, square [], and round (). A string with interleaving braces should return false. | |
isBalanced2('(foo { bar (baz) [boo] })') // true | |
isBalanced2('foo { bar { baz }') // false | |
isBalanced2('foo { (bar [baz] } )') // false | |
2. // uniq - Takes an array of numbers, and returns the unique numbers. Can you do it in O(N) time? | |
uniq([]) // [] | |
uniq([1, 4, 2, 2, 3, 4, 8]) // [1, 4, 2, 3, 8] | |
3. // intersection - Find the intersection of two arrays. Can you do it in O(M+N) time (where M and N are the lengths of the two arrays)? | |
intersection([1, 5, 4, 2], [8, 91, 4, 1, 3]) // [4, 1] | |
intersection([1, 5, 4, 2], [7, 12]) // [] | |
4. // assignDeep - Like Object.assign, but merges objects deeply. | |
assignDeep({ a: 1 }, {}) // { a: 1 } | |
assignDeep({ a: 1 }, { a: 2 }) // { a: 2 } | |
assignDeep({ a: 1 }, { a: { b: 2 } }) // { a: { b: 2 } } | |
assignDeep({ a: { b: { c: 1 }}}, { a: { b: { d: 2 }}, e: 3 }) // { a: { b: { c: 1, d: 2 }}, e: 3 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment