## MAGMA Interview Question for Software Engineer / Developers

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

1. Generally in BinarySearch algorithm we do 2 comparisions

``````while(stat < end )
{
if( A[mid == KeyElem )
{
-----
}
else if( A[mid] < KeyElem )
{
------
}
else
{
----
}
------
}// end of While``````

2. He asked me to use single comparision in While loop .

3. code for this .

``````int BinSearch(int A[], int n , int key )
{
if( n <= 0 )
{
}
else
{
int start = 0 ;
int end = n - 1;
int mid;

while(start < end)
{
mid = (start + end) / 2 ;
if( A[mid] > key )
{
start = mid + 1;
}
else
{
end = mid ;
}
}
if( ( start == end ) && ( A[start] == key ))
{
return start;
}
else
{
}
}
}``````

Comment hidden because of low score. Click to expand.
0

There are still 2 comparisions in while loop

Comment hidden because of low score. Click to expand.
0

agree, there are still two comparisons.

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

Here is the Single Comparision Code:

``````int BinSearch(int A[], int n , int key )
{
int s = 0;
int e = n-1;
int m;

while(s<e)
{
m = (s+e)/2;
if(key<=A[m])
{
e = m;
}
else
{
s = m+1;
}
}
if(A[e] == key)
{
printf("Key %d present at location %d\n", key, e);
return e;
}
else
{
printf("Key Not Present in List\n");
return -1;
}
}``````

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

Isnt a<=b just shorthand for a<b || a==b?

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

Isnt a<=b just shorthand for a<b || a==b?

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.

### 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.