Guest User

Untitled

a guest
Jan 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. **Question: 1**
  2. 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
  3. array is a subset of 1st array, or False if not.
  4.  
  5. **Answer:**
  6. import java.util.Arrays;
  7. import java.util.HashSet;
  8.  
  9. public class SubsetExample {
  10.  
  11. public static boolean isSubset_simple(String arr1[],String arr2[]) {
  12. int i = 0;
  13. int j = 0;
  14. int m = arr1.length;
  15. int n = arr2.length;
  16. for (i = 0; i < n; i++) {
  17. for (j = 0; j < m; j++) {
  18. if (arr2[i].equals(arr1[j])) {
  19. break;
  20. }
  21. }
  22.  
  23. if (j == m) {
  24. System.out.println("Value of j "+j);
  25. System.out.println("Value of m "+m);
  26. return false;
  27. }
  28. }
  29. return true;
  30. }
  31.  
  32.  
  33. public static boolean isSubset_hash(String arr1[], String arr2[]){
  34. int m = arr1.length;
  35. int n = arr2.length;
  36. HashSet<String> hset= new HashSet<>();
  37.  
  38. for(int i = 0; i < m; i++){
  39. if(!hset.contains(arr1[i]))
  40. hset.add(arr1[i]);
  41. }
  42.  
  43. for(int i = 0; i < n; i++) {
  44. if(!hset.contains(arr2[i]))
  45. return false;
  46. }
  47. return true;
  48. }
  49.  
  50.  
  51.  
  52. public static void main(String[] args) {
  53. String arr1[] = {"A", "B", "C", "D", "E"};
  54. String arr2[] = {"A", "D", "Z"};
  55.  
  56. if (isSubset_simple(arr1, arr2)) {
  57. System.out.println("True");
  58. } else {
  59. System.out.println("False");
  60. }
  61.  
  62. }
  63. }
  64.  
  65. **Question: 2**
  66. What is the computational complexity of your answer in Question 1? Can you explain why?
  67.  
  68. **Answer**
  69. I'm using O(m*n) time complexity and hashing method to getting a result. I thought that was much easier for me.
  70.  
  71.  
  72. **Question: 3**
  73. Write a function that takes an array of integers as input. For each integer, output the next fibonacci number. Solution that
  74. work both cpu and memory efficient are appreciated.
  75.  
  76. **Answer**
  77.  
  78. import java.util.HashMap;
  79. import java.util.Map;
  80.  
  81. public class FibonacciExample {
  82.  
  83. //Check the number is perfect square or not.
  84. static boolean isPerfectSquare(int x) {
  85. int s = (int) Math.sqrt(x);
  86. return (s * s == x);
  87. }
  88.  
  89. // Returns true if n is a Fibonacci Number, else false
  90. static boolean isFibonacci(int n) {
  91. return isPerfectSquare(5 * n * n + 4)
  92. || isPerfectSquare(5 * n * n - 4);
  93. }
  94.  
  95. // find the index of fibonacci number
  96. static int findIndex(int n) {
  97. if (n <= 1) {
  98. return n;
  99. }
  100. int a = 0, b = 1, c = 1;
  101. int res = 1;
  102. while (c < n) {
  103. c = a + b;
  104. res++;
  105. a = b;
  106. b = c;
  107. }
  108. return res;
  109. }
  110.  
  111. // find the fibonacci number by Index
  112. static int fib(int n) {
  113. if (n <= 1) {
  114. return n;
  115. }
  116. return fib(n - 1) + fib(n - 2);
  117. }
  118.  
  119. // Find the next fibonacci number
  120. public static int getNextFib(int n) {
  121. int[] fibs = new int[100];
  122. int i = 0;
  123. fibs[0] = 0;
  124. fibs[1] = 1;
  125. for (i = 2; i <= n + 1; i++) {
  126. fibs[i] = fibs[i - 1] + fibs[i - 2];
  127. if (fibs[i] > n) {
  128. return fibs[i];
  129. }
  130. }
  131.  
  132. if (fibs[i - 1] < n) {
  133. return fibs[i - 1] + n;
  134. }
  135.  
  136. if(fibs[i-1]== n){
  137. return fibs[i-1]+ fibs[i-2];
  138. }
  139.  
  140. if (fibs[i] == n - 1 || fibs[i] == n) {
  141. if (n != 0) {
  142. if (n == 1) {
  143. return 2;
  144. } else {
  145. return fibs[i + 1] + fibs[i + 2];
  146. }
  147. } else {
  148. return 1;
  149. }
  150. }
  151.  
  152. if (fibs[i - 1] == 1) {
  153. return fibs[n + n];
  154. }
  155. return n;
  156. }
  157.  
  158. //Use hashmap
  159. public static Map<String, String> nextFibonacci(int[] num) {
  160. int len = num.length;
  161. Map<String, String> fibNum = new HashMap<>();
  162. for (int i = 0; i < len; i++) {
  163. if (isFibonacci(num[i])) {
  164. // Using a index of given number and find the next fibonacci number.
  165. // int index = findIndex(num[i])+1;
  166. // int nextFibNum = fib(index);
  167. int nextFibNum = getNextFib(num[i]);
  168. fibNum.put(String.valueOf(i), String.valueOf(nextFibNum));
  169. } else {
  170. fibNum.put(String.valueOf(i), String.valueOf(num[i]) + " is not Fibonacci Number");
  171. }
  172. }
  173. return fibNum;
  174. }
  175.  
  176. public static void main(String[] args) {
  177. int[] numbers = {1, 21, 8};
  178. for(String nums: nextFibonacci(numbers).values()){
  179. System.out.println(nums);
  180. }
  181. }
  182. }
  183.  
  184. **Question: 4**
  185. Please explain what is the bug in the following Javascript function, and how to correct it.
  186.  
  187. function createArrayOfFunctions(y) {
  188. var arr = [];
  189. for(var i = 0; i<y; i++) {
  190. arr[i] = function(x) { return x + i; }
  191. }
  192. return arr;
  193. }
  194.  
  195. **Answer:**
  196.  
  197. function createArrayOfFunctions(y) {
  198. var arr = [];
  199. for (let i = 0; i < y; i++) {
  200. arr[i] = function(x) {
  201. return x + i;
  202. }
  203. }
  204. return arr;
  205. }
  206.  
  207. To use const / let instead of var, which makes each closure pointing to the block scope variable, i.
Add Comment
Please, Sign In to add comment