Adobe Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

How about this:

int binarySearch(void* arr, int size, int n, void* pTarget, int (*compare)(const void*, const void*))
{
	if(arr == NULL || n < 1) return -1;

	int left = 0, right = n - 1, mid, res;
	while(left <= right){
		mid = left + ((right - left) >> 1);
		res = compare(arr + (mid * size), pTarget);
		if(res == 0) return mid;
		else if(res < 0) left = mid + 1;
		else right = mid - 1;
	}
	return -1;
}

- uuuouou May 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Looks about right.
What would the compare function look like?

- puneet.sohi May 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int compare( void * arr, void * target){
	char *s = (char *)arr;
	char *d = (char *)target;	
	return memcmp( s, d, sizeof(d) );  //will sizeof work? else pass size to compare
}

- nharryp November 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Looks good but I am not able to compile it.
res = compare(arr + (mid * size), pTarget);
(Arithmatic on a pointer to void)

- Sanyam Jain May 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can u share more questions from ur interview plz?

- prateek May 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's not ANSI C, yet it works in GNU C where operations on void* is taken as char*. To make it meet ANSI, you may change that sentence to this:

res = compare((void*)((char*)arr + mid * size), pTarget);

- uuuouou May 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use templates in c it will work

- goelrinku90 August 25, 2014 | 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