Amazon Interview Question
Quality Assurance EngineersCountry: India
Interview Type: Written Test
// 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;
}
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);
}
}
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}
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;
}
/**
*
* @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);
}
#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;
}
/*
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) )
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
#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
#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
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
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);
}
}
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
#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 January 25, 2017