## Facebook Interview Question for Software Developers

Country: United States

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

The output seems to be wrong I guess

After first transformation , we get (3,-1,4,-1,2) , the output is 7
After second transformation , we get (3,4,-1,2,-1) , the output is 8

We can use Kadane's algorithm to find the max sum subarray

Comment hidden because of low score. Click to expand.
0

I thought same at first but I guess they are considering array from position 1 so the case will be L-1 and R-1.
(I guessed that from the test case given)

Comment hidden because of low score. Click to expand.
0

Using Kadane's for each query, we will end up with time O(q*n). What if q and n are of order 10^5. Can we do better in time?

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

``````Output given seems to be wrong

After first query the array is (3,-1,4,-1,2), output is 7
After second query the array is (3,4,-1,2,-1), output is 8``````

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

The Li and Ri don't start at 0. They start at 1. So the indices are Li-1 and Ri-1. After that it's a normal 'return the maximum continuous sum of an array'.

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

This is very good exercise to learn about segment trees.
once you build segment tree, for each query, do following;
L = query(0, l-1), M = query(l, r), R = query(r+1, n-1);
since the subarray between 'l' & 'r' is being reversed, all you got to do is swap, prefix and suffix sum for M.
answer will be L + M + R.

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

Here's an improved version of the question along with the answer in PHP :

``````//You are given an array A of size N and Q queries.
//For each query, you are given two indices of the array L and R.
//The subarray generated from L to R is reversed.
//Your task is to determine the maximum contiguous sum of the subarrays.
//Ex :
//5 2
//3 -1 4 2 -1
//3 4
//1 2
//Given array is {3,-1,4,2,-1}.
//For first query L=3 and R=4. Now the array becomes {3,-1,2,4,-1}.
//Maximum sub-array sum is 8 and the sub-array is {3,-1,2,4}.
//For second query L=1 and R=2. Now the array becomes {-1,3,4,2,-1}.
//Maximum sub-array sum is 9 and the sub-array is {3,4,2}.
function maxSubArray(\$array,\$startTrans,\$endTrans){
\$newArray=array();
for(\$i=0;\$i<sizeof(\$array);\$i++){
if(\$i == \$startTrans-1){
for(\$j=\$endTrans-1;\$j>=\$i;\$j--)
\$newArray[]=\$array[\$j];
\$i=\$endTrans-1;
}else{
\$newArray[]=\$array[\$i];
}
}
\$max=0;\$start=0;\$end=0;\$sum=0;
for(\$i=0;\$i<sizeof(\$newArray);\$i++){
\$sum+=\$newArray[\$i];
if(\$sum>\$max){
\$max=\$sum;
\$end=\$i;
}
if(\$sum<0){
\$sum=0;
\$start=\$i+1;
}
}
return \$max;
}``````

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

build segment tree geeksforgeeks/maximum-subarray-sum-given-range/
As pointed out earlier, do following query of segment tree
L = query(0, l-1), M = query(l, r), R = query(r+1, n-1);
since the subarray between 'l' & 'r' is being reversed, all you got to do is swap, prefix and suffix sum for M.

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

The problem statement says left inclusive and right inclusive.
The example works with zero indexed too.
Another note, the array goes back to normal after each query, so we can iterate array from 0->Li, Ri->Li, (Ri+1)->length.
It looks to be Kadane's algorithm with small modification.

``````import java.util.*;
public class Solution {

static int subArraySumWithFlip(int arr[], int l, int r) {
int max_so_far = Arrays.stream(arr).max().getAsInt();
int max_sum = max_so_far;
for (int i=0; i<l; i++) {
max_so_far = Math.max(arr[i], max_so_far + arr[i]);
max_sum = Math.max(max_sum, max_so_far);
}
for (int i=r; i>=l; i--) {
max_so_far = Math.max(arr[i], max_so_far + arr[i]);
max_sum = Math.max(max_sum, max_so_far);
}
for (int i=r+1; i<arr.length; i++) {
max_so_far = Math.max(arr[i], max_so_far + arr[i]);
max_sum = Math.max(max_sum, max_so_far);
}
return max_sum;
}
public static void main(String args[]){
int[] arr = new int[]{5,-5,-2,4,1};
System.out.println(subArraySumWithFlip(arr,0,2));
}
}``````

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.

### 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.