Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
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.
Solution using Dynamic Programming
public static int maxSubsequenceAfterSwap(int[] arr, int k) {
int count = 0;
int startIndex = 0;
int maxLen = 0, maxIndex = 0;
Queue<Integer> positionQ = new LinkedList<Integer>();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
if (count < k) {
positionQ.add(i);
count++;
} else {
if (i - startIndex > maxLen) {
maxLen = i - startIndex;
maxIndex = startIndex;
}
startIndex = positionQ.remove() + 1;
positionQ.add(i);
}
}
}
print("Max len=" + maxLen + ", starting index=" + maxIndex);
return maxLen;
}
Solution using DP
public static int maxSubsequenceAfterSwap(int[] arr, int k) {
int count = 0;
int startIndex = 0;
int maxLen = 0, maxIndex = 0;
Queue<Integer> positionQ = new LinkedList<Integer>();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
if (count < k) {
positionQ.add(i);
count++;
} else {
if (i - startIndex > maxLen) {
maxLen = i - startIndex;
maxIndex = startIndex;
}
startIndex = positionQ.remove() + 1;
positionQ.add(i);
}
}
}
print("Max len=" + maxLen + ", starting index=" + maxIndex);
return maxLen;
}
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;
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApp3
{
class Program
{
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++;
}
while (n > k)
{
if (arr[wL] == 0) n--;
wL++;
}
if ((wR - wL) > maxLen)
{
maxLen = wR - wL +1;
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 - k + oneC);
}
}
public static void Main(String[] args)
{
int[] arr = new int[20]; //{ 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1 };
Console.WriteLine("How many elements you want to enter?");
int z= Convert.ToInt32(Console.ReadLine());
int i;
Console.Write("Input elements in the array :\n");
for (i = 0; i < z; i++)
{
Console.Write("element - {0} : ", i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Value of K is: ");
int k = Convert.ToInt32(Console.ReadLine());
int result = getMaxLen(arr, k);
Console.WriteLine(result);
}
}
}
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;
}
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));
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApp3
{
class Program
{
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++;
}
while (n > k)
{
if (arr[wL] == 0) n--;
wL++;
}
if ((wR - wL) > maxLen)
{
maxLen = wR - wL +1;
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 - k + oneC);
}
}
public static void Main(String[] args)
{
int[] arr = new int[20]; //{ 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1 };
int i;
Console.Write("Input 17 elements in the array :\n");
for (i = 0; i < 17; i++)
{
Console.Write("element - {0} : ", i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Value of K is: ");
int k = Convert.ToInt32(Console.ReadLine());
int result = getMaxLen(arr, k);
Console.WriteLine(result);
}
}
}
what if everything is zero?
- Anonymous April 11, 2017