Apple Interview Question for SDE1s


Country: United States




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

We won't use sum to calculate the mean values as the sum of all values could overflow the integer range. Use the following formula for it.
Scan through the array and at each step calculate the mean as
M(n+1) = M(n) * n/(n+1) + A[n+1]/(n+1)

- kr.neerav February 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It won't work here !

- HIts January 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why?

- ManuPop February 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's a good solution for an array in external memory. But it only gives an approximate result (because of division) where an exact result might exist.
I would also propose to use not a single value on every step, but a buffer of values (say, 10 values) to minimize data read time:
M(10*(n+1)) = M(10*n)/10 + (A[10*n] + ...+A[10* + 9])/ (10*(n+1))

- Alex M. January 28, 2017 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If it's really mean value (not median), then there is no choice rather than traverse through both of the arrays. So it is not faster than O(n). But if it is median, then it could be done in O(logn).

- Alex February 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How can the median be found in O(logn)?

- B February 13, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 votes

Median can be found in O(1) for ordered array, no?

- ML June 13, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ML for a single sorted array - sure. For two sorted arrays you have to run a modified quick search to find a median.

- Alex M. January 28, 2017 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public double find(int A[], int sa, int ea, int B[],int sb, int eb, int k){
int lengthA = ea - sa + 1;
int lengthB = eb - sb + 1;

if(lengthA <= 0) return B[sb + k -1];
if(lengthB <= 0) return A[sa + k -1];
if(k <= 1) return A[sa] > B[sb]?B[sb]:A[sa];

int amid = (lengthA)/2;
int bmid = (lengthB)/2;
int current = lengthA/2+lengthB/2 + 1;

amid = sa + amid;
bmid = sb + bmid;

if(A[amid] >= B[bmid]){
if(current >= k){
return find(A, sa, amid-1, B, sb, eb, k);
}
else{
return find(A, sa, ea, B, bmid + 1,eb, k-(lengthB/2)-1);
}
}
else{
if(current >= k){
return find(A, sa, ea, B, sb, bmid - 1, k);
}
else{
return find(A, amid+1,ea,B, sb, eb, k-(lengthA/2)-1);
}
}
}

public double findMedianSortedArrays(int A[], int B[]) {
if(A.length == 0 && B.length == 0) return 0;
if(A.length == 0){
if(B.length%2 != 0)
return B[B.length/2];
else
return (B[B.length/2] + B[B.length/2-1])/2.0;
}
if(B.length == 0){
if(A.length%2 != 0)
return A[A.length/2];
else
return (A[A.length/2]+A[A.length/2-1])/2.0;
}

if((A.length + B.length)%2 == 0)
return (find(A, 0, A.length-1, B,0,B.length-1, (A.length+B.length)/2+1)+ find(A,0,A.length-1, B,0,B.length-1, (A.length+B.length)/2)) /2.0;
else
return find(A,0,A.length-1, B,0,B.length-1, (A.length+B.length)/2+1);
}

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

What is k?

- akshaycj47 November 06, 2015 | 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