Question: 1 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.
Answer:
import java.util.Arrays;
import java.util.HashSet;
public class SubsetExample {
public static boolean isSubset_simple(String arr1[],String arr2[]) {
int i = 0;
int j = 0;
int m = arr1.length;
int n = arr2.length;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (arr2[i].equals(arr1[j])) {
break;
}
}
if (j == m) {
System.out.println("Value of j "+j);
System.out.println("Value of m "+m);
return false;
}
}
return true;
}
public static boolean isSubset_hash(String arr1[], String arr2[]){
int m = arr1.length;
int n = arr2.length;
HashSet<String> hset= new HashSet<>();
for(int i = 0; i < m; i++){
if(!hset.contains(arr1[i]))
hset.add(arr1[i]);
}
for(int i = 0; i < n; i++) {
if(!hset.contains(arr2[i]))
return false;
}
return true;
}
public static void main(String[] args) {
String arr1[] = {"A", "B", "C", "D", "E"};
String arr2[] = {"A", "D", "Z"};
if (isSubset_simple(arr1, arr2)) {
System.out.println("True");
} else {
System.out.println("False");
}
}
}
Question: 2 What is the computational complexity of your answer in Question 1? Can you explain why?
Answer I'm using O(m*n) time complexity and hashing method to getting a result. I thought that was much easier for me.
Question: 3 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.
Answer
import java.util.HashMap;
import java.util.Map;
public class FibonacciExample {
//Check the number is perfect square or not.
static boolean isPerfectSquare(int x) {
int s = (int) Math.sqrt(x);
return (s * s == x);
}
// Returns true if n is a Fibonacci Number, else false
static boolean isFibonacci(int n) {
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}
// find the index of fibonacci number
static int findIndex(int n) {
if (n <= 1) {
return n;
}
int a = 0, b = 1, c = 1;
int res = 1;
while (c < n) {
c = a + b;
res++;
a = b;
b = c;
}
return res;
}
// find the fibonacci number by Index
static int fib(int n) {
if (n <= 1) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
// Find the next fibonacci number
public static int getNextFib(int n) {
int[] fibs = new int[100];
int i = 0;
fibs[0] = 0;
fibs[1] = 1;
for (i = 2; i <= n + 1; i++) {
fibs[i] = fibs[i - 1] + fibs[i - 2];
if (fibs[i] > n) {
return fibs[i];
}
}
if (fibs[i - 1] < n) {
return fibs[i - 1] + n;
}
if(fibs[i-1]== n){
return fibs[i-1]+ fibs[i-2];
}
if (fibs[i] == n - 1 || fibs[i] == n) {
if (n != 0) {
if (n == 1) {
return 2;
} else {
return fibs[i + 1] + fibs[i + 2];
}
} else {
return 1;
}
}
if (fibs[i - 1] == 1) {
return fibs[n + n];
}
return n;
}
//Use hashmap
public static Map<String, String> nextFibonacci(int[] num) {
int len = num.length;
Map<String, String> fibNum = new HashMap<>();
for (int i = 0; i < len; i++) {
if (isFibonacci(num[i])) {
// Using a index of given number and find the next fibonacci number.
// int index = findIndex(num[i])+1;
// int nextFibNum = fib(index);
int nextFibNum = getNextFib(num[i]);
fibNum.put(String.valueOf(i), String.valueOf(nextFibNum));
} else {
fibNum.put(String.valueOf(i), String.valueOf(num[i]) + " is not Fibonacci Number");
}
}
return fibNum;
}
public static void main(String[] args) {
int[] numbers = {1, 21, 8};
for(String nums: nextFibonacci(numbers).values()){
System.out.println(nums);
}
}
}
Question: 4 Please explain what is the bug in the following Javascript function, and how to correct it.
function createArrayOfFunctions(y) {
var arr = [];
for(var i = 0; i<y; i++) {
arr[i] = function(x) { return x + i; }
}
return arr;
}
Answer:
function createArrayOfFunctions(y) {
var arr = [];
for (let i = 0; i < y; i++) {
arr[i] = function(x) {
return x + i;
}
}
return arr;
}
To use const / let instead of var, which makes each closure pointing to the block scope variable, i.