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 )
	{
		return -1; // key not found
	}
	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
		{
			return -1; // key not found
		}
	}
}

- siva.sai.2020 February 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

There are still 2 comparisions in while loop

- Messi February 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

agree, there are still two comparisons.

- siva.sai.2020 April 26, 2015 | Flag
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;
	}
}

- SRB February 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous February 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous February 18, 2011 | Flag Reply


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