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?

- Anonymous April 11, 2017 | Flag Reply
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.

- ssk April 11, 2017 | Flag Reply
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;
		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;
	}

- Ag April 13, 2017 | Flag Reply
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;
		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;

}

- Ag April 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i didn't understood the question

- bapan April 14, 2017 | Flag Reply
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;

}

- Mika April 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}
}
}

- Sandeep Potdukhe October 18, 2022 | Flag Reply
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;

}

- divm01986 April 12, 2017 | Flag Reply
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));
	}

- Anon April 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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);
}
}
}

- Sandeep Potdukhe October 18, 2022 | 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