Very simple logic

public class Solution {
// DO NOT MODIFY THE LIST. IT IS READ ONLY
public ArrayList searchRange(final List A, int B) {
int l =0;
int h = A.size()-1;
int mid = (l+h)/2;
int f = -1;
while(l<=h){
mid = (l+h)/2;
if(A.get(mid)==B)
{
f=mid;
break;
}
else if(A.get(mid)>B)
h = mid -1;
else
l = mid +1;
}
ArrayList ans = new ArrayList();
if(f==-1){
ans.add(-1);
ans.add(-1);
return ans;
}
int x = f;
int y = f;
if(A.get(0)==B)
x =0;
else{
l = 0;
h = f ;
while(l<=h){
mid = (l+h)/2;
if(l!=0 && A.get(mid-1)<B && A.get(mid)==B)
{
x = mid;
break;
}
else if(A.get(mid)==B)
h = mid -1;
else
l = mid +1;
}
}

    if(A.get(A.size()-1)==B)
    y = A.size()-1;
    else{
        l = f;
        h = A.size()-1;
    while(l<=h){
        mid = (l+h)/2;
        if(h!=A.size()-1 && A.get(mid+1)>B && A.get(mid)==B)
        {
            y = mid;
            break;
        }
        else if(A.get(mid)==B)
        l = mid  + 1;
        else
        h = mid - 1;
    }
    }
ans.add(x);
ans.add(y);
return ans;
}

}

Click here to start solving coding interview questions