Amazon Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Phone Interview

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

what if everything is zero?

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

Not necessarily... The question says, k swap operations are allowed. If you closely look at the solution, the answer to the input is 9 and not 10. But your code will return 10 as the longest subarray starting with start_pos=1 and end_pos=10. Consider another case, [0,0,0,0,0], k=3. Your code would return 3. But the actual answer is 0 as there are no 1s to swap with 0s.

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

Solution using Dynamic Programming

``````public static int maxSubsequenceAfterSwap(int[] arr, int k) {
int count = 0;
int startIndex = 0;
int maxLen = 0, maxIndex = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
if (count < k) {
count++;
} else {
if (i - startIndex > maxLen) {
maxLen = i - startIndex;
maxIndex = startIndex;
}
startIndex = positionQ.remove() + 1;
}
}
}

print("Max len=" + maxLen + ", starting index=" + maxIndex);
return maxLen;
}``````

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

Solution using DP

``````public static int maxSubsequenceAfterSwap(int[] arr, int k) {
int count = 0;
int startIndex = 0;
int maxLen = 0, maxIndex = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
if (count < k) {
count++;
} else {
if (i - startIndex > maxLen) {
maxLen = i - startIndex;
maxIndex = startIndex;
}
startIndex = positionQ.remove() + 1;
}
}
}

print("Max len=" + maxLen + ", starting index=" + maxIndex);
return maxLen;``````

}

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

i didn't understood the question

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

Recursive brute force.

``````static int maxOneSequence(int[] array, int swapsAllowed)
{
if (swapsAllowed == 0)
{
return countSequenceLen(array);
}

int maxLen = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] == 1)
{
for (int j = 0; j < array.Length; j++)
{
if (array[j] == 0)
{
int len =  maxOneSequence(createSwappedArray(array, i, j), swapsAllowed - 1);
if (len > maxLen) { maxLen = len; }
}
}
}
}
return maxLen;``````

}

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

``````public int maxSubarray(int[] arr,int k){
int max = 0;
int diff = 0;//num zeros
int totalOnes = 0;
for(int i = 0; i < arr.length; i++){
if(arr[i] == 1){
totalOnes++;
}
}
int j = 0;
int i = 0;
while(j < arr.length){
if(arr[j]  == 0){
diff++;
}else{
totalOnes--;
diff--;
}
while(diff > k){
if(arr[i] == 1){
diff++;
totalOnes++;
}else{
diff--;
}
}
if(totalOnes >= k){
max = Math.max(max,j - i + 1);

}
j++;

}
return max;

}``````

Comment hidden because of low score. Click to expand.
-2
of 2 vote

The answer is the length of the largest sliding window possible, with 'K' zeros.

You also need to handle the case, where number of 1s outside the window should be greater or equal to 'K'.

``````public static int getMaxLen(int[] arr, int k){
int wL = 0, wR = 0;
int n = 0;
int maxLen = 0;
int maxWL = 0, maxWR = 0;
if(arr[wR] == 0) n =1;
while(wR < arr.length-1){
if(n <= k){
wR++;
if(arr[wR] == 0) n++;
}
if(n > k){
if(arr[wL] == 0) n--;
wL++;
}

if((wR - wL) > maxLen){
maxLen = wR - wL;
maxWL = wL;
maxWR = wR;
}
}
int oneC = 0;
for(int i = 0; i<maxWL; i++){
if(arr[i] == 1) oneC++;
}
for(int i = maxWR+1; i<arr.length; i++){
if(arr[i] == 1) oneC++;
}
if(oneC >= k)
return maxLen;
else{
return (maxLen - oneC);
}
}

public static void main(String[] args){
int[] arr= {0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0};
System.out.println(getMaxLen(arr, 3));
}``````

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.