FactSet Research Systems, Inc Interview Question
Software Engineer / DevelopersTeam: Market data
Country: United States
Interview Type: Phone Interview
Hats off to the above logic given by anonymous.. i just wrote the code for it..
int a[]={2,3,2,3,2};
int x=0,y;
int sum=0,product=1;
for (int i=0;i<5;i++){
x^=a[i];
sum+=a[i];
product*=a[i];
}
y=(sum-3*x)/2;
System.out.println("x "+x);
System.out.println("y "+y);
if ( (Math.pow(x, 3) * Math.pow(y,2)) == product )
System.out.println("True");
else
System.out.println("False");
Counter examples: [2,2,3,3,6] [3,3,10,10,15], etc.
Your methods also fails if 0 is allowed, as in
[0,1,1,1,1]. Your method does not distinguish between 5 of a kind and a full house, though to be fair, neither did the question. It's probably best to have asked the interviewer. To minimize complexity (not maximize cleverness), you might as well just count how many distinct values there are, as well as how many of each value. You can return false in less than one complete pass, depending on the input.
1. if you xor all the numbers then the no which occurs 3 times can be obtained, now an efficient way needs to be found out for finding the 2nd no.
2. when you get the odd no of times occurring element then again XOR each element with this element , there should be 3 zeros now and two similar nos.
0,0,0, 001, 001 ( 4^5 = 001)
3. Now again XOR all the elements with each other if the result == 0 (001^001 == 0) then return true
But this approach requires 3 passes so not an efficient one.
If anyone can suggest a better solution
boolean isConditionTrueForArray(int[] inputArr) {
int firstCount = 0;
int secondCount = 0;
int firstLetter = inputArr[0];
int secondLetter;
for ( int i = 1; i < 5; i++ ) {
if ( firstLetter != array[i]) {
secondLetter = array[i];
break;
}
}
for ( i = 0; i < 5; i++) {
if ( firstLetter == inputArr[i] ) {
firstLetterCount++;
} else if ( secondLetter == inputArr[i] ) {
secondLetterCount++;
} else {
return false;
}
}
if ( (firstLetterCount == 3 && secondLetterCount == 2)
|| (firstLetterCount == 2 && secondLetterCount == 3) ) {
return true;
} else {
return false;
}
}
import java.util.*;
class Counter {
public Counter(int a[]) {
Arrays.sort(a);
int tester[]=a;
int i=0;
if (tester[i]==tester[i+1]){
if (tester[i+1]==tester[i+2]) {
if (tester [i+3]== tester [i+4]) {
System.out.println("Three of a kind");
}
else
System.out.println("Format incorrect");
}
else if(tester[i+2]==tester[i+3]){
if (tester[i+3]==tester[i+4]) {
System.out.println("Three of a kind");
}
else
System.out.println("Format incorrect");
}
}
else
System.out.println("Format incorrect");
}
}
public class TwoThree {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a={2,3,2,3,4};
Counter obj= new Counter(a);
}
}
A little too many if elsem but it does it in O(N).
Logic:
-Start with storing first new number, increment counter for first new number.
-Store the second new number and increment the counter for second number.
-If there is a third new number return False, if the next number encountered is same as the first or second numbers, increment respective counters.
-At the end check for valid counter numbers or else return false.
def formatCorrect(inArray):
first = -1
firstCount = 0
second = -1
secondCount = 0
for num in inArray:
if first != -1:
if num == first:
firstCount += 1
elif second != -1:
if num == second:
secondCount +=1
else:
#print firstCount, secondCount
return False
else:
second = num
secondCount += 1
else:
first = num
firstCount += 1
#print "First Number is "+str(first)+" with count "+str(firstcount)+"."
#print "Second Number is "+str(second)+" with count "+str(secondcount)+"."
if (firstCount == 3 and secondCount == 2) or (firstCount == 2 and secondCount == 3):
#print firstCount, secondCount
return True
else:
#print firstCount, secondCount
return False
a mixted strategy solution based on the comments before
bool isCount2And3(int A[]){
int *num1 = NULL, *num2 = NULL;
int sum = A[0];
for(int i=1; i<5;++i){
sum+=A[i];
if(!num1) num1=A+i;
if(*num1 == A[i]) continue;
if(!num2) num2=A+i;
if(num2 && *num2!=A[i]) return false;
}
return sum == (*num1)*3+(*num2)*2 || sum == (*num1)*2 + (*num2)*3;
}
Dim a As Array = {4, 5, 4, 5, 4}
Dim i As Integer
Dim count As Integer = 0
Dim secCount As Integer = 0
For i = 0 To a.Length - 1
If (a(0).Equals(a(i))) Then
count = count + 1
End If
Next
For i = 0 To a.Length - 1
If Not (a(0).Equals(a(i))) Then
For k = 0 To a.Length - 1
If (a(i).Equals(a(k))) Then
secCount = secCount + 1
End If
Next
Exit For
End If
Next
If ((count = 3 And secCount = 2) Or (count = 2 And secCount = 3)) Then
Return True
Else
Return False
End If
Dim a As Array = {4, 5, 4, 5, 4}
Dim i As Integer
Dim count As Integer = 0
Dim secCount As Integer = 0
For i = 0 To a.Length - 1
If (a(0).Equals(a(i))) Then
count = count + 1
End If
Next
For i = 0 To a.Length - 1
If Not (a(0).Equals(a(i))) Then
For k = 0 To a.Length - 1
If (a(i).Equals(a(k))) Then
secCount = secCount + 1
End If
Next
Exit For
End If
Next
If ((count = 3 And secCount = 2) Or (count = 2 And secCount = 3)) Then
Return True
Else
Return False
End If
def first(list):
count = 0
dict = {}
for item in list:
if item not in dict.keys():
dict[item] = 1
else:
val = dict[item]
dict[item] = val + 1
if len(dict.keys()) <= 2:
for item in dict:
if dict[item] == 2 or dict[item] == 3:
return True
else:
return False
else:
return False
value = first([4,4,4,5,4])
if value:
print("successful")
else:
print("unsuccessful")
def first(list):
count = 0
dict = {}
for item in list:
if item not in dict.keys():
dict[item] = 1
else:
val = dict[item]
dict[item] = val + 1
if len(dict.keys()) <= 2:
for item in dict:
if dict[item] == 2 or dict[item] == 3:
return True
else:
return False
else:
return False
value = first([4,4,4,5,4])
if value:
print("successful")
else:
print("unsuccessful")
XOR all the elements and at the same time in that pass calculate the sum and also the product. After XOR the element which occurs 3 times will be left. Suppose that element is x and the element which occurs 2 times is y.
- Anonymous October 10, 2011so 3x + 2y = sum this way we can get y
if the values obtained satisfy x^3 * y^2 = product then the array satisfies the property.
I think this works in a single pass and no extra space. If anyone can get a counter example do tell me