Microsoft Interview Question
Software Engineer / Developerssubtract N from all...now read again if there is a value that reads 0 then there was a N there...otherwise the value that is the maximum in negative is the closest smallest to N...(-1 is greater than -2)
3 2 1 9 8 5 7
N=6
subtract N gives
-3 -4 -5 4 3 -1 1
read again and the index which has -1 is the answer
i mean you dont have to read again just store the -ve value with the index in 2 temp variables ...the one with the max negative value on the numberline is the one
int
myFunc(int *in,int size,int N)
{
int flag=0;
int cNo=-1;
for(int i=0;i<size;i++)
{
if (in[i]==N) {
cNo=i;break;
}
else if (in[i] < N && flag==0)
{
cNo =in[i];
flag=1;
}
if(in[i] < N && flag ==1) {
cNo= (in[i]>cNo) ? in[i]:cNo;
}
}
return cNo;
}
int main()
{
int in[7]={13,5,8,2,6,9,10};
int d= myFunc(in,sizeof(in)/sizeof(int),11);
printf("%d is the answer\n",d);
char c=getchar();
}
check this out babes.
I don't get the question.
- Anonymous February 16, 2007A one-pass scan will do the job. Or I miss something?