Skip to content

Instantly share code, notes, and snippets.

@themoralpanda
Created March 27, 2018 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themoralpanda/b3e22c895bf068ccd698286e7037a677 to your computer and use it in GitHub Desktop.
Save themoralpanda/b3e22c895bf068ccd698286e7037a677 to your computer and use it in GitHub Desktop.
Oursky Developer Pretest - haivicky@gmail.com
/*
Program to check if an array is a subset of another array
*/
public class answer1{
public static void main(String args[]){
char [] a ={'A','B','C','D','E'};
char [] b ={'A','D','D','Z'};
System.out.println(isSubset(a,b));
}
private static boolean isSubset(char[] a, char []b){
//Hashset is a Set implementation that uses Hashmap and stores values without duplicates
HashSet <Character> hSet = new HashSet<Character>();
//Store all the elements of major array that is to be searched
for(char c:a){
hSet.add(c);
}
//Loop through the second array and return false, if any one of its elements is not found in the original array
for(char c:b){
if(hSet.contains(c)!=true)
return false;
}
//This section will reach only if the second array is the subset of the first array.
return true;
}
}

Time complexity is: O(n)

  • The first loop takes O(n) to insert all the elements in the Hashset

  • The second loop takes O(n) to loop through the query array, to check if the elemets are present in Hashset.

  • So O(n) + O(n) = O(n)

The worst case time complexity of my algorithm is still O(n)

import java.util.*;
public class answer3{
public static void main(String args[]){
int [] c = {1,9,22};
nextFibonacci(c);
}
private static void nextFibonacci(int[] arr){
//Copy the initial array.
int arr2[] = arr.clone();
//HashMap is used with Key as the value in the array and initial value as 0
HashMap<Integer,Integer> hMap = new HashMap<Integer,Integer>();
// Initialize the HashMap with key as array values and value as 0;
for(int val:arr){
hMap.put(val, 0);
}
//Sort the input array and setup the initial parameters for the Fibonacci series.
Arrays.sort(arr);
int a=0,b=1,c=0;
int count =0;
int i=0;
//The below loop runs untill all the required fibonacci numbers for the entire input array is found.
do{
while(c<=arr[i]){
c = a+b;
a=b;
b=c;
}
hMap.put(arr[i],c);
i++;
count++;
}while(count<arr.length);
//Loop through the backup array and find the corresponding Fibonacci values from the Hashmap.
for(int key:arr2){
System.out.println(hMap.get(key));
}
}
}
/*
The Bug in the program is,
function createArrayOfFunctions(y) {
var arr = [];
for(var i = 0; i<y; i++) {
arr[i] = function(x) { return x + i; }
}
return arr;
}
The value of i is always y.
lets say, if we createArrayOffunctions(3), when the function is executed, we will be given
arr(3)
0 ->function(x)
1 ->function(x)
2 ->function(x)
if you call one of this function eg, arr[0](3) - it will return, 3+3 = 6.
but I believe, what they want is 3+0 = 3.
So the solution is
*******************
we can write a closure or a self executing function to trap the variable i inside each function.
*/
function createArrayOfFunctions(y)
{
var arr = [];
for(var i = 0; i<y; i++) {
arr[i] = (
function(i){
return function(x) {
return x + i;
}
})(i)
} return arr; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment