Amazon Interview Question for Quality Assurance Engineers


Country: India
Interview Type: Written Test




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

public static int maxOnes(int[] arr){
		int count = 0;
		int max = 0;
		for(int i = 0; i < arr.length; i++){
			if(arr[i] == 0)
				count = 0;
			else
				count++;
			max = count > max ? count : max;
		}
		return max;
	}

- Anonymous January 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Time :O(n), Space :O(1)
	public static int maxNumberOf1s(String str) {
		if (null == str || str.isEmpty() || !str.contains("1")) {
			return 0;
		}
		int count = 0;
		int curCount = 0;
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == '1') {
				if (curCount == 0) {
					curCount = 1;
				} else {
					curCount++;
				}
				count = Math.max(curCount, count);
			} else {
				curCount = 0;
			}
		}

		return count;
	}

- Raj January 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please check the following code.

public class Consecutive01 {
    
    static int[] a= {0,1,1,0,0,0,1,1,0,0,1,0};
    
    public static void main(String[] args) {
        int sum = 0;
        int max  = 0 ;
        for(int i = 0 ; i< a.length ; i++){
           if(a[i] == 0){
               sum = 0;
           } else {
               sum++;
               if(sum > max)
                   max = sum;
           }
        }
        
        System.out.println("Max consecutive 1s is: "+max);
    }
    
}

- cleonjoys January 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int getMaxOfConsecutiveOnes(String s) {
    int globalMax = 0;
    int currentMax = 0;

    for (int i = 0; i < s.length(); i++) {
      if (s.charAt(i) == '1') {
        currentMax++;
      } else {
        if (currentMax != 0) {
          if (globalMax < currentMax) {
            globalMax = currentMax;
          }
          currentMax = 0;
        }
      }
    }

    // check the end
    if (globalMax < currentMax) {
      globalMax = currentMax;
    }

    return globalMax;
  }

Tests:

{"00110001001110", 3},
  {"1000010001", 1},
  {"00000", 0},
  {"11111", 5}

- Mike L January 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def maxConsequtiveOnes(String):
    if String is None or String.strip() is "" :
        return 0
    _max = 0
    _ones = []
    for i in String:
        if i == "1":
            _max += 1
        if i == "0":
            _max = 0
        _ones.append(_max)
    return max(_ones)

- revanthpobala January 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int maxConsecutive(int[] array) {
int count = 0;
int temp = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == 1) {
count++;
if (count > temp)
temp = count;
} else {
count = 0;
}
}
return temp;
}

- Tarun January 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int maxConsecutive(int[] array) {
		int count = 0;
		int temp = 0;
		for (int i = 0; i < array.length; i++) {
			if (array[i] == 1) {
				count++;
				if (count > temp)
					temp = count;
			} else {
				count = 0;
			}
		}
		return temp;
	}

- Tarun January 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int maxConsequtiveOnes(int[] arr)
{
int max = 0;
int temp = 0;
for(int i = 0; i < arr.length; i++)
{
if(arr[i] == 1)
{
temp++;
}
else
{
temp = 0;
}
max = Math.max( temp, max );
}
return max;
}

- Rupak Sah January 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int maxConsequtiveOnes(int[] arr)
   {
      int max = 0;
      int temp = 0;
      for(int i = 0; i < arr.length; i++)
      {
         if(arr[i] == 1)
         {
            temp++;
         }
         else
         {
            temp = 0;
         }
         max = Math.max( temp, max );
      }
      return max;
   }

- Rupak Sah January 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C++ solution. Very fast. Early out when run length exceeds remaining search space.

#include <vector>
#include <iostream>

template<typename IteratorType, typename ValueType>
size_t findLongestRun(IteratorType start, IteratorType end, ValueType comparand)
{
	size_t result = 0;

	auto ptr = start;

	while(end - ptr > result)
	{
		if(*ptr == comparand)
		{
			auto runEnd = ptr;

			for(;;)
			{
				if(runEnd == end)
					break;

				if(*runEnd != comparand)
					break;

				runEnd++;
			}

			size_t length = runEnd - ptr;

			if(length > result)
				result = length;

			ptr = runEnd;
		}
		else
		{
			ptr++;
		}
	}

	return result;
}

int main(void)
{
	std::vector<int> values {1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1};
	auto result = findLongestRun(values.cbegin(), values.cend(), 1);

	std::cout << result << std::endl;

	return 0;
}

- snichols@therealm.io January 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
 *
 * @author prizkabon
 */
public class MaximunConsecutive1 {
    
static void getMaxOnes(int[] theArray)
{
 ArrayList<Integer> cont_ones = new ArrayList();
 int tmpCont=0;
 
    
 for(int i=0;i<theArray.length;i++)
 {
   if(theArray[i]==1)
   {   
       tmpCont++;
        
    
   }else
   {
    cont_ones.add(tmpCont);
    tmpCont=0;
    
   }
    
  
 }
 
 
 System.out.println("The max ones is:"+Collections.max(cont_ones));

}    
    
    
    
}

public static void main(String[] args) {
int arrayToCheckOnes[] = {1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,1,1,1,1,1,0,0,0}; 
     MaximunConsecutive1.getMaxOnes(arrayToCheckOnes);
}

- prizkaboom January 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

template <typename T, typename FindType>
int FindMaxConsecutiveOne(const T& data, const FindType findValue)
{
	int maxLen = 0, curLen = 0;
	for (auto element : data)
	{
		if (findValue == element)
		{
			curLen++;
			maxLen = std::max(curLen, maxLen);
		}
		else
		{
			curLen = 0;
		}
	}
	return maxLen;
}


int main()
{
	std::string data1 = "00110001001110";
	std::cout << FindMaxConsecutiveOne(data1, '1') << std::endl;
	std::string data2 = "11111";
	std::cout << FindMaxConsecutiveOne(data2, '1') << std::endl;
	std::vector<int> data3{ 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0 };
	std::cout << FindMaxConsecutiveOne(data3, 1) << std::endl;

    return 0;
}

- psychoria January 29, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* 
Classic Kerninghan & Ritchie
Use states 
   - in_1s
   - max_1s 
   - current_count of 1s   
*/
def count_max_1s( arr ){
  // max_1s , in_1s, current_count 
  #(max_1s,in_1s) = fold ( arr, [ 0, false ,0 ] ) -> {
    #(max_1s , in_1s, current_count) = $.p 
    if ( $.o == 0 ){
      if ( in_1s ){
        if ( current_count > max_1s ){
          max_1s = current_count 
        }
        current_count = 0 
        in_1s = false 
      }
    } else {
      current_count += 1
      in_1s = true 
    }
    [ max_1s, in_1s, current_count ]
  } 
  max_1s // return 
}
arr = [0,0,1,1,0,0,0,1,0,0,1,1,1,0]
println ( count_max_1s(arr) )

- NoOne January 29, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) Keep a running counter C (indicating subsequent length of 1s seen) =0
2) Iterate over the array:
When you see a 1, increment C. Check if it's the max if we have seen so far (if so update it).
When you see a 0, reset C to 0.

return the max.

- codingwarrior01 January 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int GetMaxConsecutiveOnes(int[] a)
{
	if (a == null || a.Length == 0)
		return 0;
	
	int count = 0;
	int max = 0;
	
	for (int i=0; i <a.Length; i++)
	{
		if (a[i] == 1)
		{
			count++;
			max = Math.Max(max, count);
		}
		else
			count = 0;
	}
	
	return max;
}

- hnatsu February 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python programme

#Find the maximum consecutive 1's in an array of 0's and 1's.
#Example:
#a) 00110001001110 - Output :3 [Max num of consecutive 1's is 3]
#b) 1000010001 - Output :1 [Max num of consecutive 1's is 1]

a = [0,0,1,1,0,0,0,1,0,0,1,1,1,0]
b = [1,0,0,0,0,1,0,0,0,1]
#a = [0, 1, 1, 0, 1, 1,1]
c = 0;
max = 0;
for i in a:
#print i
if i == 1:
c += 1;
# print c
if max < c:
max = c
else:
if max < c:
max = c
c = 0
else:
c = 0

print max

- Raghavendra poojari February 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#Find the maximum consecutive 1's in an array of 0's and 1's.
#Example:
#a) 00110001001110 - Output :3 [Max num of consecutive 1's is 3]
#b) 1000010001 - Output :1 [Max num of consecutive 1's is 1]

a = [0,0,1,1,0,0,0,1,0,0,1,1,1,0]
b = [1,0,0,0,0,1,0,0,0,1]
#a = [0, 1, 1, 0, 1, 1,1]
c = 0;
max = 0;
for i in a:
	#print i
	if i == 1:
		c += 1;
	#	print c
		if max < c:
			max = c
	else:
		if max < c:
			max = c
			c = 0
		else:
			c = 0

print max

- Raghavendra poojari February 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

php solution

- Anonymous January 11, 2022 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#Find the maximum consecutive 1's in an array of 0's and 1's.
#Example:
#a) 00110001001110 - Output :3 [Max num of consecutive 1's is 3]
#b) 1000010001 - Output :1 [Max num of consecutive 1's is 1]

a = [0,0,1,1,0,0,0,1,0,0,1,1,1,0]
b = [1,0,0,0,0,1,0,0,0,1]
#a = [0, 1, 1, 0, 1, 1,1]
c = 0;
max = 0;
for i in a:
	#print i
	if i == 1:
		c += 1;
	#	print c
		if max < c:
			max = c
	else:
		if max < c:
			max = c
			c = 0
		else:
			c = 0

print max

- Raghavendra poojari February 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
					
public class Program
{
	public static void Main()
	{
		// a) 00110001001110 - Output :3 [Max num of consecutive 1's is 3]
        var input = "1100100001";
	
		var max = 0;
		var consecutiveOnes = 0;

		foreach (var c in input) {

			if (c == '1') {
				consecutiveOnes++;
				
				max = Math.Max(max, consecutiveOnes);
			} else {
				consecutiveOnes = 0;
			}
		}
		
		Console.WriteLine(max);
	}
}

- Ange February 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
					
public class Program
{
	public static void Main()
	{
		// a) 00110001001110 - Output :3 [Max num of consecutive 1's is 3]
        var input = "1100100001";
	
		var max = 0;
		var consecutiveOnes = 0;

		foreach (var c in input) {

			if (c == '1') {
				consecutiveOnes++;
				
				max = Math.Max(max, consecutiveOnes);
			} else {
				consecutiveOnes = 0;
			}
		}
		
		Console.WriteLine(max);
	}

}

- Ange February 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a="00110001001011111"
def con(a):
c=0;max=0
for i in a:
# print(i)
if int(i) == 1:
c+=1
if max < c:
max = c
else:
c=0


print("max",max)

con(a)

- Vivek Dubey March 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MaxConsecutite1s {
public static void main(String[] args) {
Integer[] arr = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int count = 0;
int maxSize = 0;
for (int index = 0; index < arr.length; index++) {
if (arr[index] == 1)
count++;
else if (arr[index] == 0 || count >= 1) {
if (maxSize < count)
maxSize = count;
count = 0;
}
}
if (maxSize < count)
maxSize = count;
System.out.println("Max consecutive 1s size:" + maxSize);
}
}

- ys May 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MaxConsecutite1s {
    public static void main(String[] args) {
        Integer[] arr = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
        int count = 0;
        int maxSize = 0;
        for (int index = 0; index < arr.length; index++) {
            if (arr[index] == 1)
                count++;
            else if (arr[index] == 0 || count >= 1) {
                if (maxSize < count)
                    maxSize = count;
                count = 0;
            }
        }
        if (maxSize < count)
            maxSize = count;
        System.out.println("Max consecutive 1s size:" + maxSize);
    }
}

- ys May 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MaxConsecutite1s {
    public static void main(String[] args) {
        Integer[] arr = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
        int count = 0;
        int maxSize = 0;
        for (int index = 0; index < arr.length; index++) {
            if (arr[index] == 1)
                count++;
            else if (arr[index] == 0 || count >= 1) {
                if (maxSize < count)
                    maxSize = count;
                count = 0;
            }
        }
        if (maxSize < count)
            maxSize = count;
        System.out.println("Max consecutive 1s size:" + maxSize);
    }

}

- ys May 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MaxConsecutite1s {
    public static void main(String[] args) {
        Integer[] arr = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
        int count = 0;
        int maxSize = 0;
        for (int index = 0; index < arr.length; index++) {
            if (arr[index] == 1)
                count++;
            else if (arr[index] == 0 || count >= 1) {
                if (maxSize < count)
                    maxSize = count;
                count = 0;
            }
        }
        if (maxSize < count)
            maxSize = count;
        System.out.println("Max consecutive 1s size:" + maxSize);
    }

}

- ys May 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my take in C#:

string strNum = "000111111110100100000111";
            int val0 = 0;
            int val1 = 0;
            int temp0 = 0;
            int temp11 = 0;

            for (int m = 0; m < strNum.Length; m++)
            {
                if (int.Parse(strNum[m].ToString()) == 0)
                {
                    val0++;                    
                }
                else
                {
                    if (val0 != 0)
                    {
                        if (temp0 < val0)
                        {
                            temp0 = val0;
                        }
                        val0 = 0;
                    }
                }

                if (int.Parse(strNum[m].ToString()) == 1)
                {
                    val1++;
                }
                else
                {
                    if (val1 != 0)
                    {
                        if (temp11 < val1)
                        {
                            temp11 = val1;
                        }
                        val1 = 0;
                    }
                }
            }

            Console.WriteLine("Number of consecutive 0's: " + temp0);
            Console.WriteLine("Number of consecutive 1's: " + temp11);

- vg0453 October 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In python

inputs are given in list format(enclose in [])

correct me if am wrong

import json
x1=input("Enter the list:")
x=json.loads(x1)

def min_consecutive_one(list1):
    count=0
    for i in range(0,(len(list1)-1)):
        if list1[i]==list1[i+1]:
            count+=1
    return count
min_consecutive_one(x)

- jagannathans92 February 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

print(max(S.split('0')))

- mubeen.electrical April 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

S="11100100001110010011000001"
M=max(S.split('0'))
print(len(M))

- mubeen.electrical April 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def maximum_consecutive_1s(string):
count = mx = 0
for i in string:
if int(i) == 1:
count += 1

if count > mx:
mx = count
else:
count = 0

return mx

- swatidares May 25, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python code -

listA=[0,1,1,1,0,0,1,0,0,1]
countvalue=0
maxValue=0

for i in range(len(listA)):
    #print(i)
    if listA[i]==1:
       countvalue=countvalue+1
    else:
       countvalue=0
    if countvalue>maxValue:
       maxValue=countvalue
    
print(maxValue)

- blankspace August 04, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a = input()

result = a.split("0")
print(len(max(result, key=len)))

- Anonymous September 26, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package Careercup;

import java.util.Arrays;
import java.util.Collections;

public class MaxCOnsecutiveOnes {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a= {0,0,1,1,1,0,0,1,1,0};
Boolean isStart=false;
Boolean isEnd=false;
int[] c=new int[a.length];
int j=0;
int count=0;
for( int i=0;i<a.length;i++)
{
if(a[i]==1&& isStart==false)
{
isStart=true;
count++;
}
else if(a[i]==1 &&isStart==true &&isEnd==false)
{
count++;
}
else if(a[i]!=1 && isStart==true)
{
isEnd=true;isStart=false;
c[j]=count;
count=0;
j++;

}
else if(a[i]==1 && isEnd==true)
{
isStart=true;isEnd=false;
count++;
}



}
int k=0,max=0;
while(k<c.length)
{
if(c[k]>max)
{
max=c[k];
}

k++;
}

System.out.println(max);
}
}

- chanthini February 17, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Kotlin way :::

fun findMaxConsecutiveOnes(nums: IntArray): Int {
        var windowLength = 0
        var count = 0
        nums.forEach {
            if (it == 0) {
                count = 0
            } else {
                windowLength = maxOf(windowLength, count)
                count++
            }
        }
        return windowLength
    }

- Thiyagu June 28, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python Code;)
#Given a binary array, find the maximum number of consecutive 1s in this array.
class Solution:
def findMaxConsecutiveOnes(self,nums:List[int])->int:
max_sum=nums[0]
counter=0
for i in range(len(nums)):
counter+=nums[i]
if(counter>max_sum):
max_sum=counter
if(nums[i]==0):
counter=0

return max_sum

- Anonymous October 18, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#Given a binary array, find the maximum number of consecutive 1s in this array.
class Solution:
    def findMaxConsecutiveOnes(self,nums:List[int])->int:
        max_sum=nums[0]
        counter=0
        for i in range(len(nums)):
            counter+=nums[i]
            if(counter>max_sum):
                max_sum=counter
            if(nums[i]==0):
                counter=0

        return max_sum

- Monu Shukla October 18, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String str = "0011000110100111011001111010110111";
		String[] arr = str.replaceAll("0+", "_").split("_");
		int max_len = 0;
		for(String s : arr) {
			max_len = Math.max(max_len, s.length());
		}
		System.out.println(max_len);

- debghosal92 August 07, 2022 | Flag Reply


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