Ebay Interview Question for Java Developers


Country: United Kingdom
Interview Type: Phone Interview




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

Are you joking ???

Ebay asked this question ???

- Nitin Gupta May 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Because it is a straight Binary Search question which is basic algorithm that first year students study in their college and a company of eBay's repute asking it, without any twist in words or twist in application of binary search.

So, I was shocked.

- Nitin Gupta May 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
4
of 4 votes

I was asked binary search at Amazon. I didn't get the code 100% correct, but I was still the best the interviewer had ever seen on this question.

- Gayle L McDowell May 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Binary search is trickier than one might think. Nitin, try this: do it yourself, with a time limit of 15 minutes (all while spending time explaining aloud what you're doing), without looking at a computer or testing your code other than by hand. Then post your code here.

There are some very subtle problems that you can have. For instance, what do you think of this method of finding the midpoint? int mid = (l+r)/2; Looks correct?

@Gayle: was your mistake limited to int mid = (low + high) / 2? In my opinion that one's barely a real mistake...

- eugene.yarovoi May 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ya it is integer overflow case.

Instead one should do like

low + (high - low) / 2

- Nitin Gupta May 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, Ebay asked this question for the position of Junior Java Developer. Can anyone suggest me if the code below is okay? It works fine but I was wondering since someone mentioned the "mid" problem I was hoping I could use some insight. Thank you. Here's the code:

static int [] arr={1,2,3,5,6,7};
	
static int input=5;

int head1=0;

int tail1=arr.length-1;
int mid1=((tail1-head1)/2);
		
binaryRecursively(mid1);

binaryRecursively(int mid) {
	if (arr[mid]==input){
		System.out.println("Found Recursively - "+arr[mid] +" at index - "+mid);
	}
	else if (arr[mid]<input){
		mid++;
		binaryRecursively(mid);
	}
	else if (arr[mid]>input){
		mid--;
		binaryRecursively(mid);
	}
		
}

- gunsnroses23k May 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

@Nitin: well, there are a couple possibilities here:

1) You just happen to be considerably more attentive to detail than almost everyone else. I believe I made the integer overflow mistake the first time I attempted this problem in a time-sensitive setting, before I knew to watch for that issue.
2) You've read about the gotchas of this problem before -- other candidates may only understand the concept, without remembering small details like not falling into the kind of trap you just avoided.
3) You might have missed the overflow issue if I hadn't remarked on it, if you had been not scrutinizing that line in particular.
4) You might have avoided that mistake, but made a different mistake if you were attempting this problem under time and social pressure.

- eugene.yarovoi May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@eugene.yaravoi :- In my initial days, I never cared about integer overflow condition. My sole purpose used to be just solve the problem. So, yes I also had made this mistake in my college days. But, later on, I learned that overflow cases can occur. So, now I've become more attentive on these kind of details also.

@ gunsnroses23k :- You should never calculate mid like above.

Consider a scenario where low = 5, high = 7.
Note that these are numerals above are representing index of array not elements of array.

Ideally - our mid should be index no 6
But according to your code, it would be 1, which is wrong.
Now you can calculate mid like this

mid = (low + high) / 2

You will get correct answer which is 6. But this can fail when the sum of low and high exceeds the integer limit defined for that data type in that language.

So, the correct way is

mid = low + (high - low) / 2

- Nitin Gupta May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ nitin Thanks for the input. I agree with your suggestion but if you look carefully what I did was not exactly binary search. I would rather call it a distorted form of binary search. The performance in my program can be slow but it does work as according to my code "high" & "low" are always constant & recursive method is invoked with argument of "mid" which is either just incremented or decremented (as I said this can be a lot slower than binary search for large arrays but it does work) The reason I did that was because I wanted to show them something different than what usually they would expect anyway if asked next time I would keep all that in my mind including the integer overflow case. Thanks for the help!

- gunsnroses23k May 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Coding binary search correctly is not as easy as Nitin Gupta puts it. IMO, he already knows it, so is unable to see why it might be a reasonable quesiton to ask. Also, even if it was an easy question, what is the harm in asking it? There are a lot of followup questions one can ask. For instance, does the code work if there are duplicates? Can you modify it to return the first occurence? Can you suggest some optimizations? etc etc.

- Anonymous May 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Use binary search

- alex May 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public int Search(int [] a, int data){
    // check if the array has no elements 
    if(a.length <=0){
        return -1;
    }
    
    int low = a[0];
    int high  = a.length -1;
    int mid ;
    
    while(low < high){
        mid = low + ((high-low)/2);
        if(a[mid] == data){
            // element found 
            return a[mid];
        }
        else if(data > a[mid]){
            // check in the upper half of the array
            low = mid+1;
        }
        else if(data < a[mid]){
            // check in the lower half of the array
            high = mid - 1;
        }
    }
    // it is not in this array
    return -1;

}

- Anonymous May 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

low=a[0] is wrong should be low=0

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

int binarySearch(int arr[], int l, int r, int ele){
if(l==r){
if(a[l]==ele)
return l;
}
if(r=l+1){
if(a[l]==ele)
return l;
else if(a[r]==ele)
return r;

else{
int mid=(l+r)/2;
if(a[mid]==ele)
return mid;
else if(a[mid]>ele)
return binarySearch(a,l,mid-1,ele);
else
return binarySearch(a,mid+1,r,ele);
}
return -1;
}

- Sandeep Goutele May 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int binarySearch(int arr[], int l, int r, int element){
if(l<=r){
int mid = (l+r)/2;
if(arr[mid]==element)
return mid;
else if(arr[mid]<element)
return binarySearch(arr,mid+1,r,element);
else
return binarySearch(arr,l,mid-1,element);
}
return -1;
}

- Sandeep Goutele May 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can anyone suggest me if the code below is okay? It works fine but I was wondering since someone mentioned the "mid" problem I was hoping I could use some insight. Thank you. Here's the code:

static int [] arr={1,2,3,5,6,7};
	
static int input=5;

int head1=0;

int tail1=arr.length-1;
int mid1=((tail1-head1)/2);
		
binaryRecursively(mid1);

binaryRecursively(int mid) {
	if (arr[mid]==input){
		System.out.println("Found Recursively - "+arr[mid] +" at index - "+mid);
	}
	else if (arr[mid]<input){
		mid++;
		binaryRecursively(mid);
	}
	else if (arr[mid]>input){
		mid--;
		binaryRecursively(mid);
	}
		
}

- gunsnroses23k May 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

mid can go forever if the value you are searching for is not in the array. so i would pass the max and min to validate if mid is not going beyond possible array index value.

else return -1 (or whatever value you use to indicated not found)

- tmsagila May 15, 2013 | Flag


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