FactSet Research Systems, Inc Interview Question
Software Engineer / Developersdef pocker_func(arr);
number_count=defaultdict(int)
for val in arr:
number_count[val]+=1
inters = set(number_count.values()).intersection(set([2,3])
if inters == set([2,3]): return True
else: return False
#include <stdio.h>
#include <conio.h>
bool func1(int a[]);
void main()
{
int a[5],i;
printf("Enter");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("\nReturns %d",func1(a));
getch();
}
bool func1(int s[5])
{
int i,j,temp=0,count=0,y=1,z=0;
printf("\nSorted Elements");
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
{
if(s[i]>s[j])
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
printf("\n%d",s[i]);
}
printf("\nDistinct Elements ");
for(i=0;i<5;i++)
{
if(s[i]!=s[i+1])
{
printf("\n %d",s[i]);
count++;
}
}
printf("\nCount=%d",count);
for(i=0;i<5;i++)
{
if(s[i]==s[i+1])
{
y++;
z=y;
}
else
{
printf("\n%d repeated %d times",s[i],y);
y=1;
}
}
if(count==2 && z>1 && z<4){
printf("\nz=%d",z);
return true;}
else
return false;
}
def pocker_func(arr);
number_count=defaultdict(int)
for val in arr:
number_count[val]+=1
inters = set(number_count.values()).intersection(set([2,3])
if inters == set([2,3]): return True
else: return False
<pre lang="" line="1" title="CodeMonkey5827" class="run-this">#include <stdio.h>
#include <conio.h>
bool func1(int a[]);
void main()
{
int a[5],i;
printf("Enter");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("\nReturns %d",func1(a));
getch();
}
bool func1(int s[5])
{
int i,j,temp=0,count=0,y=1,z=0;
printf("\nSorted Elements");
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
{
if(s[i]>s[j])
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
printf("\n%d",s[i]);
}
printf("\nDistinct Elements ");
for(i=0;i<5;i++)
{
if(s[i]!=s[i+1])
{
printf("\n %d",s[i]);
count++;
}
}
printf("\nCount=%d",count);
for(i=0;i<5;i++)
{
if(s[i]==s[i+1])
{
y++;
z=y;
}
else
{
printf("\n%d repeated %d times",s[i],y);
y=1;
}
}
if(count==2 && z>1 && z<4){
printf("\nz=%d",z);
return true;}
else
return false;
}
</pre><pre title="CodeMonkey5827" input="yes">
</pre>
1) The return can be true only if there are only 2 distinct integers coming in the input array.
2) Use a 2x2 array to store the number and frequency of the number.
3) After the scan through the input, see if the frequency matches the required condition for a true return.
public bool PokerFun(int[] inputArray)
{
int[][] containerArray = new int[2][];
for (int i = 0; i < 2; i++)
{
containerArray[i] = new int[2];
for (int j = 0; j < 2; j++)
{
containerArray[i][j] = 0;
}
}
for (int i = 0; i < inputArray.Length; i++)
{
if (containerArray[0][0] != 0 && containerArray[0][0] == inputArray[i])
{
containerArray[0][1]++;
}
else if (containerArray[0][0] == 0)
{
containerArray[0][0] = inputArray[i];
containerArray[0][1]++;
}
else if (containerArray[1][0] != 0 && containerArray[1][0] == inputArray[i])
{
containerArray[1][1]++;
}
else if (containerArray[1][0] == 0)
{
containerArray[1][0] = inputArray[i];
containerArray[1][1]++;
}
else
{
return false;
}
}
if ((containerArray[0][1] == 2 || containerArray[0][1] == 3)
&& (containerArray[1][1] == 2 || containerArray[1][1] == 3))
{
return true;
}
return false;
}
Test input arrays:
int[] inputArray1 = new int [] { 1, 1, 2, 2, 2 };
int[] inputArray2 = new int[] { 1, 2, 2, 2, 2 };
int[] inputArray3 = new int[] { 1, 1, 3, 2, 2 };
int[] inputArray4 = new int[] { 1, 1, 2, 5, 4 };
int[] inputArray5 = new int[] { 1,3, 2, 4, 5 };
Worked well with the tests I have.
class Counter {
public Counter(int a[]) {
int first=a[0],flag=0,flag2=0,second=0;
for (int i=1;i<5;i++) {
if (first==a[i])
flag++;
else{
if (flag2==0)
second=a[i];
if(second==a[i])
flag2++;
else
break;
}
}
if ((flag==1&&flag2==3)||(flag==2&&flag2==2))
System.out.println("Three of a kind");
else
System.out.println("Incorrect format");
}
}
public class TwoThree {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a={1,2,1,2,2};
Counter obj= new Counter(a);
}
}
- kamal August 25, 2011