eBay Interview Question

How would you pick the middle element of a list

Interview Answers

Anonymous

Nov 6, 2012

its a list (assuming not an array).... Take two pointer....increment 1st pointer by 2 everytime & second by 1 [Rabbit & tortoise problem] if 1st pointer(rabbit) reaches the end of the list.... the position of the tortoise is the location of the middle element of the list.

8

Anonymous

Jul 14, 2015

You guys are overthinking this var arr = [1,2,3,4,5,6,7]; console.log(arr[Math.floor((arr.length - 1) / 2)]);

4

Anonymous

Oct 30, 2012

int[] intArray = {12,34,56,78,98,31,47}; int startIndex=0; int endIndex=intArray.length -1 ; while(startIndex

1

Anonymous

Sep 26, 2012

I said by dividing the length with 2. He said there would be two passes involved with that (?). He wanted some way with one pass.

3