eBay Interview Question: How would you pick the middle... | Glassdoor

eBay

## Interview Question

Front End Software Engineer Interview San Jose, CA

# How would you pick the middle element of a list

2

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.

Interview Candidate on Sep 25, 2012
1

int[] intArray = {12,34,56,78,98,31,47};

int startIndex=0;
int endIndex=intArray.length -1 ;

while(startIndex<endIndex) {
startIndex++;
endIndex--;
}

if(startIndex == endIndex)
int middleElement = intArray[startIndex];
//you will have only one middle element

else {

int middleElement1 = intArray[startIndex];
int middleElement2 = intArray[endIndex];
//you will have two middle elements.
}

Sudha on Oct 29, 2012
8

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.

Jayanth on Nov 5, 2012
1

You guys are overthinking this

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

Josh on Jul 14, 2015