Last active
June 11, 2019 19:11
-
-
Save manjarb/9cff165044dbe824e273febd6eb81f2c to your computer and use it in GitHub Desktop.
Varis Darasirikul's Pre-Test 2019
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
| // Write a function that takes two arrays as input, | |
| // each array contains a list of A-Z; | |
| // Your program should return True if the 2nd array is a subset of 1st array, or False if not. | |
| function isSubset(source, subset) { | |
| let isShared = true; | |
| // Create an object which will be used for store formatted subset object | |
| // in order to reduce the duplicated string for a linear search later | |
| let sourceObject = {}; | |
| let subsetObject = {}; | |
| for (let i = 0; i < source.length; i++) { | |
| sourceObject[source[i]] = 1; | |
| } | |
| for (let i = 0; i < subset.length; i++) { | |
| subsetObject[subset[i]] = 1; | |
| } | |
| // Linear search for finding subset string | |
| for (let key in subsetObject) { | |
| if (!(key in sourceObject)) { | |
| isShared = false; | |
| } | |
| } | |
| return isShared; | |
| } | |
| console.log(isSubset(['A','B','C','D','E'], ['A','E','D'])); // true | |
| console.log(isSubset(['A','B','C','D','E'], ['A','D','Z'])); // false | |
| console.log(isSubset(['A','D','E'], ['A','A','D','E'])); // true | |
| // Big-O notation explain | |
| // The minumum amount of loop for this function is (source.length * subset.length) + (source.length + subset.length) | |
| // ex. isSubset(['A','B','C','D','E'], ['A','D','Z']) = (5 * 3) + (5 + 3) = 23 times | |
| // if we increase input size from (3, 3) into (6, 6) the result time cosuming will be n ^ 2 | |
| // That is mean for Big-O notation,we got O(n ^ 2) (Because the new sizing is ^2 from the original sizing) |
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
| // Using only if, else, while, and add(x, y), write 3 functions to implement *, -, / operation. | |
| // You are not allowed to use the operator of * - /, and you are provided with the add function, | |
| // NEGATIVE_ONE -1 value and ONE holding 1 value. | |
| const NEGATIVE_ONE = -1; | |
| const ONE = 1; | |
| function add(a, b) { | |
| return a + b | |
| } | |
| function multiply(a, b) { | |
| let result = a; | |
| for (let i = 1; i < b; i++) { | |
| result = add(result, a); | |
| } | |
| return result; | |
| } | |
| function minus(a, b) { | |
| let result = a | |
| for (let i = 0; i < b; i++) { | |
| result = add(result, NEGATIVE_ONE); | |
| } | |
| return result; | |
| } | |
| function divide(a, b) { | |
| let counter = 0; | |
| if (a === 0) { | |
| return 0; | |
| } | |
| if (b === 0) { | |
| return 'Not a number' | |
| } | |
| if ((a % b) === 0) { | |
| let counter = 0; | |
| while (a > 0) { | |
| for (let i = 0; i < b; i++) { | |
| a = add(a, NEGATIVE_ONE); | |
| } | |
| counter = add(counter, ONE); | |
| } | |
| return counter; | |
| } else { | |
| return 'error'; | |
| } | |
| } | |
| console.log(minus(6, 78) , ' :answer is -72'); // -72 | |
| console.log(minus(94, 17) , ' :answer is 77'); // 77 | |
| console.log(minus(10, 5) , ' :answer is 5'); // 5 | |
| console.log(multiply(7, 3), ' :answer is 21'); // 21 | |
| console.log(divide(14, 7), ' :answer is 2'); // 2 | |
| console.log(divide(36, 9), ' :answer is 4'); // 4 | |
| console.log(divide(48, 4), ' :answer is 12'); // 12 | |
| console.log(divide(15, 7), ' :answer is error'); // error |
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
| // Write a function that takes an array of integers as input. | |
| // For each integer, output the next fibonacci number. | |
| // Solution that work both cpu and memory efficient are appreciated. | |
| // Fibonacci number of Fn is defined by: | |
| // Fn = Fn-1 + Fn-2 | |
| // F1 = 1, F2 = 1 | |
| // For example: | |
| // nextFibonacci([1,22,9]) | |
| // Output: | |
| // 2 | |
| // 34 | |
| // 13 | |
| function nextFibonacci(array) { | |
| const sortArray = array.slice().sort((a, b) => a < b) | |
| let fibArray = [1, 1]; | |
| let i = 2; | |
| let fibCalculate = 0; | |
| while(fibCalculate < sortArray[0]) { | |
| fibCalculate = fibArray[i - 2] + fibArray[i - 1]; | |
| fibArray.push(fibCalculate); | |
| i++; | |
| } | |
| for (let j = 0;j < array.length; j++) { | |
| for (let k = 0;k < fibArray.length; k++) { | |
| if (array[j] < fibArray[k]) { | |
| console.log(fibArray[k]); | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| console.log('Input is 1, 22, 9, 150'); | |
| console.log('Answer is 2, 34, 13, 233'); | |
| nextFibonacci([1, 22, 9, 150]) // 2, 34, 13, 233 |
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
| // Please explain what is the bug in the following Javascript function, and how to correct it. | |
| // The problem is i value will always equal to y - 1 | |
| // because when we call the function it will always reference to last i value (because i is hoisting into a near function scope) | |
| function createArrayOfFunctions(y) { | |
| var arr = []; | |
| for (var i = 0; i < y; i++) { | |
| arr[i] = function(x) { | |
| return x + i; | |
| } | |
| } | |
| return arr; | |
| } | |
| // Correct version | |
| function correctCreateArrayOfFunctions(y) { | |
| var arr = []; | |
| // change from var to let to prevent for loop hoisting variable | |
| for (let i = 0; i < y; i++) { | |
| arr[i] = function(x) { | |
| return x + i; | |
| } | |
| } | |
| return arr; | |
| } | |
| const funcArray = createArrayOfFunctions(10); | |
| console.log('Original Function'); | |
| console.log(funcArray[1](2), ' :correct value is 3'); | |
| console.log(funcArray[4](6), ' :correct value is 10'); | |
| console.log(funcArray[8](13), ' :correct value is 21'); | |
| console.log('-------------------------------------'); | |
| console.log('Modify Function'); | |
| const correctFuncArray = correctCreateArrayOfFunctions(10); | |
| console.log(correctFuncArray[1](2), ' :correct value is 3'); | |
| console.log(correctFuncArray[4](6), ' :correct value is 10'); | |
| console.log(correctFuncArray[8](13), ' :correct value is 21'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment