Directi Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Phone Interview
how you are sorting and making the bucet in range in linear time..
?
Can you elaborate that
I am not sorting and I'm not actually making any bucket. The buckets are used for algorithm explanation.
You basically need the following operations:
- find x - the smallest element in input vector that (x >= 1 and x < 2) - easily done in O(N) with a simple pass through the array
- take the smallest element & second smallest element from the input array (and ignore the first element from triple) - doable in O(N)
- find x the highest element s.t. (x >= 0.5 and x < 1) - O(N)
- etc.
To generalize, the algorithm uses operations like: find the first/second lowest/highest element which lies in the interval [a..b). These operations are doable in O(N).
@anonymous - Yup he used those constraints
@max - There must be a solution present if he asked
public static Boolean funct(double[] arr) {
Integer count =0;
for(int i=0;i<arr.length; i++){
if(arr[i] < 2 ){
count++;
for(int j=i+1;j<arr.length; j++){
if(arr[j] < 2 && arr[i]+arr[j]< 2.0){
count++;
for(int x=j+1;x<arr.length; x++){
if(arr[i] < 2 ){
if(arr[i]+arr[j] + arr[x] <2.0 && arr[i]+arr[j] + arr[x] >= 1.0&& count ==2 ){
System.out.println(arr[i]);
System.out.println(arr[j]);
System.out.println(arr[x]);
x=arr.length;
return true;
}
}
}
j=arr.length;
}
}
i=arr.length;
}
}
return false;
}
//***************TEST EXAMPLE********************
public static void main(String[] args) {
double[] x={1,1.1,0.3,3,2,1,1,2,0.2,0.2,6,7};
Boolean b;
b=funct(x);
if(b == false)
System.out.println("Unable to find the sum of the triplet is in the range (1, 2)");
}
}
My understanding, if there are real numbers the function should be:
boolean funct(double[] arr) or float.
if you notice., j=arr.length;
i=arr.length;
These 2 commands will ensure that this code will not run more than O(n)
I might be wrong too.......please repost the code with whitespaces....will help to understand better
so the algorithm is trying a 3 pairs of numbers in the array - right? Imagine you have 10 elements in the array. How many times do you consider the last element of the array?
Consider adding iteration counters to help you see why this solution is very far from O(N).
@Anonymous on February 17, 2013
I have put iterations in and it never goes above o(n).. but if you want then replace j=arr.length; & i=arr.length with "return false"
I hadn't noticed the early returns. In that case your solution is not correct - consider {0, 0, 0.9, 0, 0.9} (or the example above {1.9,0.2,0.5,0.36}).
i was just responding to your incorrect comments about the complexity.
i know that.. {1.9,0.2,0.5,0.36}..I will work on the fix later
It doesn't find solution with input array = {0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.4, 0.4, 0.4}.
num=[float(x) for x in raw_input().split(' ')]
a=[x for x in num if 0< x < 1]
b=[x for x in num if 1<= x < 2]
a.sort()
min3a=a[:3]
max3a=a[len(a)-3:]
if len(a)>=3:
if 2<=sum(min3a):
print None
elif 1 < sum(min3a) < 2:
print min3a
if 1<sum(max3a):
for i in range(0,4):
if 1<sum(min3a[:i]+max[i:])<2:
print min3a[:i]+max[i:]
if len(b)>=1 and len(a)>=2:
if 1<sum(min3a[0:2]+[min(b)])<2:
print min3a[0:2]+[min(b)]
Take a integer variable and set last 3 LSB for each 0 obtained, and 4-6 LSB for each 1.
Then check if those bits are set and if yes return true.
BOOL findtriplet(int* arr)
{
int count = 0;
while(arr[i] != '\0')
{
if(a[i] == 0)
count = (count | 1) << 1;
if(a[i] == 1)
count = ((count&7) | ((count | 8) << 1));
if((count & 7) | (count & 56))
return true;
}
return false
}
I think we can do it in nlogn by first sorting it, then check if a[i-1]+a[i]+a[i+1] is in range(1,2) from i=(1 to lastIndex-1)
This wont work either,you are just checking for consecutive triplets..ex {0.1,0.2,0.3,0.4,1.4}.your solution will return false but there exists a triplet i.e {0.1+0.4+1.4=1.9<2}
Then you would have to check all combinations of triplets and it can not be done in O(n) time. But in this case, question states that the array contains only positive numbers and my solution is correct.
Consider the following buckets: (0..0.5), [0.5..1), [1..2), [2..inf).
Obviously, we ignore numbers in [2..inf).
Now basically we need to treat all cases of choosing 3 numbers from the 3 buckets.
We only need to look at the following cases (the other cases are "worse" or are covered by these):
1. If possible, choose the smallest element from bucket [1..2) => for the 2nd and 3rd we need to take the smallest 2 elements available. If sum < 2 then return true.
2. If possible, choose the two smallest elements from bucket [0.5 .. 1) => for the 3rd we need to take the smallest element available. If sum < 2 then return true;
3. If possible, choose the highest element from bucket [0.5 .. 1) => if possible, for the 2nd and 3rd take the highest and the second highest from bucket (0 .. 0.5). If sum > 1 then return true.
4. If possible, choose the highest 3 elements from bucket (0..0.5). If sum > 1 then return true.
If none of the cases above found a solution then return false.
Space complexity: O(1), you don't need to explicitly store numbers in buckets.
Time complexity: each operation (e.g.: find smallest element from bucket [1..2), etc.) can be done in O(N). There is a constant number of these operations => overall complexity O(N)
LATER EDIT:
Since the answer was down-graded without any question or explanation why it would be wrong, here is the actual code and the associated tests. Hopefully I didn't forget any relevant test case.
The code could be optimized more and be more condensed, but I tried to make it as clear as possible (regarding to the explanations above and the space and time requirements).
- cristian.botau February 22, 2013