FactSet Research Systems, Inc Interview Question for Software Engineer / Developers


Team: Market data
Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
1
of 1 vote

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.

so 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

- Anonymous October 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

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");

- Tabs October 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- survived October 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if((a[0]^a[1]^a[2]==0)&&(a[3]^a[4] == 0))
return true;
else
return false

- yogi September 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

one correction
if((a[0]^a[1]^a[2]==a[0])&&(a[3]^a[4] == 0))
return true;
else
return false

- andrew September 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the numbers can be in any positions, the array is not sorted.

- cinderella September 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- cinderella September 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if((a[0]^a[1]^a[2]^a[3]^a[4]==a[0])||(a[0]^a[1]^a[2]^a[3]^a[4]==a[4]))
return 1
else
return 0

- Nascent Coder September 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Declare 2 variables min/max and two variable count_min = count_max =0
Set min/max in one traversal
Traverse array and increment count_min and count_max on min/max occurances
if((count_min == 2 && count_max == 3) || (count_min == 3 && count_max == 2))
return 1;

return 0;

- Learner September 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
    }

}

- Anonymous October 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}
}

- Anonymous December 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isSame(int arr[5], const int size){
    int M[10] = {0};
    bool validThree = false, validTwo = false;
    
    for(int i = 0; i < size; ++i){
    	M[arr[i]]++;
    }
    
    for(int i = 0; i < 10; ++i){
    	if(M[i] == 3){
    		validThree = true;
    	}
    	if(M[i] == 2){
    		validTwo = true;
    	}
    }
    return validThree && validTwo;
}

- Instigo April 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Maitrey Soparia May 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- nano April 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Hepsi September 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Hepsi September 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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")

- Anonymous November 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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")

- anonymous November 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

if(a[0] == a[1] && a[3] == a[4])
    return true;
else
    return false;
}

- Cubby September 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if(a[3]^a[4] == 0)
return true;
else
return false

- Anonymous September 13, 2011 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More