Facebook Interview Question
348 Interview Reviews |
Back to all Facebook Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Facebook:
FInd the maximum sum of a sub-sequence from an positive integer array where any two numbers of sub-sequence are not adjacent to each other in the original sequence. E.g 1 2 3 4 5 6 --> 2 4 6
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (14)
0 of 3 people found this helpful
For example: this sequence:
1, -1, -2, -3, 3
the max is 4 .
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
function getMaxSubsequence(arr) {
var maxFrom = {};
function getMax(start, end) {
var len = end-start+1, x1, maxSkip1, maxSkip2;
if (maxFrom[start]===undefined) {
if (len<1) {
return 0;
} else if (len==1) {
return arr[start];
} else if (len==2) {
return Math.max(arr[start],arr[end]);
} else {
x1 = arr[start],
maxSkip1 = getMax(start+1,end),
maxSkip2 = getMax(start+2,end);
maxFrom[start] = Math.max(x1+maxSkip2,maxSkip1);
}
}
return maxFrom[start];
};
return getMax(0, arr.length-1);
}
Helpful Answer?
Yes |
No
Inappropriate?
max[i] = Math.max(nums[i] + max[i + 2], max[i + 1], max[i + 2]);
Helpful Answer?
Yes |
No
Inappropriate?
1 of 4 people found this helpful
Then the solution is :
int sum = 0;
int index = array.length - 1;
sum = array[index] + array[index - 2] + array[index - 4]
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
int[] a = {5,3,5,1,8};
int[] b = {2,4,6,9,2};
int res = MaxSequence(a, b, 0, 0);
System.out.println(res);
}
public int MaxSequence(int[] a, int[] b, int i, int j) {
if(i==a.length-1 && j== b.length-1) {
return max(a[i],b[j]);
} else if(i>=a.length-1 && j <= b.length-1) {
return sum(b,j);
}
else if(i== a.length-1 && j< b.length-1) {
return max(a[i],sum(b,j));
} else if(i<a.length-1 && j==b.length-1) {
return max(sum(a,i),b[j]);
}
else {
return max(a[i]+MaxSequence(a, b, i+1, j+1),b[j]+MaxSequence(a, b, i+2, j+1));
}
}
public int sum(int[] a, int i) {
int sum=0;
while(i<a.length) {
sum+= a[i];
i++;
}
return sum;
}
public int max(int a, int b) {
if (a>b)
return a;
else
return b;
}
Helpful Answer?
Yes |
No
Inappropriate?
int max_sum(int arr[], int sz, int indx) {
if(indx >= sz) return 0;
if(dp[indx] != -1) return dp[indx];
int a = max_sum(arr, sz, indx+1);
int b = max_sum(arr, sz, indx+2);
if(a > (b+arr[indx])) dp[indx] = a;
else dp[indx] = b+arr[indx];
return dp[indx];
}
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
simple O(n^2) solution is:
void print(vint &a)
{
copy(a.begin(),a.end(),ostream_iterator<int>(cout," "));
cout<<endl;
}
void print_seq(vint &a, vint &P, int s)
{
int i=s;
int p=-1;
stack<int> out;
while(p!=i)
{
out.push(a[i]);
p=i;
i=P[i];
}
while(!out.empty())
{
cout<<(out.top())<<" ";
out.pop();
}
cout<<endl;
}
void sequence(vint &a)
{
vint L;
vint P;
int n=a.size();
L.insert(L.begin(),n,0);
P.insert(P.begin(),n,0);
L[0]=a[0]; P[0]=0;
L[1]=a[1]; P[1]=1;
for(int i=2;i<n;++i)
{
P[i]=i; L[i]=a[i];
for(int j=0;j<i-1;++j)
{
if(a[i]+L[j]>L[i])
{
P[i]=j;
L[i]=L[j]+a[i];
}
}
}
int max,maxind;
int i=0;
max=L[0];maxind=0;
for(i=0;i<n;++i)
{
if(L[i]>max)
{
max=L[i];
maxind=i;
}
}
print_seq(a,P,maxind);
cout<<"sum: "<<max<<endl;
}
Helpful Answer?
Yes |
No
Inappropriate?
void subseq_opt(vint &a)
{
vint L;
vint P;
int n=a.size();
L.insert(L.begin(),n,0);
P.insert(P.begin(),n,0);
L[0]=a[0]; P[0]=0;
L[1]=a[1]; P[1]=1;
for(int i=2;i<n;++i)
{
if(a[i]+L[i-2] > L[i-1])
{
L[i]=a[i]+L[i-2];
P[i]=P[i-2];
}
else
{
L[i]=L[i-1];
P[i]=P[i-1];
}
}
return L[L.size()-1];
}
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
int find_opt(int *a, int n)
{
if(n==1) return a[0];
if(n==2) return max(a[0],a[1]);
int l[2];
l[0]=a[0];
l[1]=max(a[0],a[1]);
int i,ii;
for(i=2;i<n;++i)
{
ii=i&1;
l[ii]=max(l[ii]+a[i],l[1-ii]);
}
ii=i&1;
return l[1-ii];
}
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up



9 of 12 people found this helpful
by Anonymous:
public int findMaxNonAdjacentSequence(int[] nums) {
int[] max = new int[nums.length + 2];
for (int i = nums.length - 1; i >= 0; i--) {
max[i] = Math.max(nums[i] + max[i + 2], max[i + 1]);
}
return max[0];
}
If memory is concern, you can just keep the last two max values.