## Amazon Interview Question for Quality Assurance Engineers

Country: India
Interview Type: Written Test

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

One of the clear implementations of the above problem is that you must count the total number of given key elements in the array and then count the number of the key elements which is present consecutively in the array, if the next element is not the key element then, simply make a counter variable 0, and at last if the value of the counter variable equals the total count then, return true else return false.

Implementation of the above code:

#include<bits/stdc++.h>
using namespace std;
bool checknumbers(int arr[], int key, int n){
int count = 0;
for(int i = 0; i < n; i++){
if(arr[i] == key)
count++;
}
int temp = 1;
for(int i = 0; (i + 1) < n; i++){
if(arr[i] == key && arr[i + 1] == key){
temp++;
if(temp == count)
return true;
}
else
temp = 1;
}
return false;
}
int main(){
int arr[] = {1, 1, 0, 1, 1, 1, 1};
cout<<checknumbers(arr, 1, 7)<<endl;
}

For example, the above code will return false because there are a total of 6 one's but all are not consecutive.

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

``````public class Prob1 {
public static void main(String[] args) {
int[] s= {1,1,0,0,1,3,4};
//     /**/   int[] s = {/*1,1,1,*/0,0,3,4};
int i=0;
boolean isOneStart = false;
boolean isOneEnd = false;
boolean isConsec = true;
while(i<s.length){

if(isOneStart==false && s[i]==1){
isOneStart = true;
}

else if(isOneStart==true && isOneEnd==false && s[i]!=1){
isOneEnd = true;
}

else if(isOneEnd== true && s[i]==1){
isConsec = false;
break;
}

i++;
}

if(isOneStart==false){
System.out.println("Array doesnt have any Ones");
} else {
if (isConsec) {
System.out.println("It is consecutive");
} else {
System.out.println("It is not consecutive");
}
}
}
}``````

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

``````public class Prob1 {
public static void main(String[] args) {
int[] s= {1,1,0,0,1,3,4};
int i=0;
boolean isOneStart = false;
boolean isOneEnd = false;
boolean isConsec = true;
while(i<s.length){

if(isOneStart==false && s[i]==1){
isOneStart = true;
}

else if(isOneStart==true && isOneEnd==false && s[i]!=1){
isOneEnd = true;
}

else if(isOneEnd== true && s[i]==1){
isConsec = false;
break;
}

i++;
}

if(isOneStart==false){
System.out.println("Array doesnt have any Ones");
} else {
if (isConsec) {
System.out.println("It is consecutive");
} else {
System.out.println("It is not consecutive");
}
}
}
}``````

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

bool ChecKConsecutive(int[] a, int key )
{
int len = a.Length;
if(len ==0)
return false;
if(len==1)
return true;
int lastIndex = -1;
for(int i=0;i<len;i++)
{
if(a[i] == key)
{
if(lastIndex != -1 && lastIndex +1 !=i)
{
return false;
}
lastIndex = i;
}
continue;
}
return true;

}

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

``````private bool CheckConsecutive(int[] a, int key)
{
int len  = a.Length;
if(len ==0)
return false;
if(len == 1 && a[0] == key)
return true;

int lastIndex =-1;

for (int i =0 ;i < len;i++)
{
if(a[i] == k)
{
if(lastIndex  != -1 &&  lastIndex  +1 == i)
{
return false;
}
lastIndex = i;
}
continue;
}
}``````

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

public static boolean checkConsecutive(int[] array, int key) {

if (array.length == 0) {
return false;
}
if (array.length == 1 && array[0] == key)
return true;
if (array.length > 1 && array[0] == key && array[1] != key)
return false;
for (int i = 1; i < array.length; i++ ){
if (array[i] == key) {
if (i + 1 >= array.length && array[i - 1] != key) {
return false;
}
if (array[i + 1] != key && array[i - 1] != key)
return false;
}
}
return true;
}

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

``````public static boolean checkConsecutive(int[] array, int key) {

if (array.length == 0) {
return false;
}
if (array.length == 1 && array[0] == key)
return true;
if (array.length > 1 && array[0] == key && array[1] != key)
return false;
for (int i = 1; i < array.length; i++ ){
if (array[i] == key) {
if (i + 1 >= array.length && array[i - 1] != key) {
return false;
}
if (array[i + 1] != key && array[i - 1] != key)
return false;
}
}
return true;``````

}

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

public static boolean checkConsecutive(int[] array, int key) {
if (array.length == 0) {
return false;
}
if (array.length == 1 && array[0] == key)
return true;
if (array.length > 1 && array[0] == key && array[1] != key)
return false;
for (int i = 1; i < array.length; i++ ){
if (array[i] == key) {
if (i + 1 >= array.length && array[i - 1] != key) {
return false;
}
if (array[i + 1] != key && array[i - 1] != key)
return false;
}
}
return true;
}

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

``````#include<stdio.h>

int ConsecutiveNumberInArray(int arr[10])
{
int i,j,k;
int count = 0;
int trac = 0;
int num;
int arr_val;
int index_chk = 0;

printf("Enter the array elements: ");
for(i=0; i<7; i++)
{
scanf("%d", &arr_val);
arr[i] = arr_val;
}

printf("The array is: ");
for(j=0; j<7; j++)
{
printf("%d  ", arr[j]);
}

printf("\n\nEnter the number to searched for consecutively: ");
scanf("%d", &num);

for(k=0; k<7; k++)
{
if((num == arr[k]) && (k == trac))
{
count++;
++trac;
printf("count = %d and trac = %d\n", count , trac);
if(count ==3)
break;
}
else
{
count = 0;
trac++;
}
}
if(count == 3)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int result;
int array[10];

result = ConsecutiveNumberInArray(array);

if(result == 1)
{
printf("Searched number appeared consecutively\n");
}
else
{
if(result == 0)
{
printf("Searched number does not appeared consucutively\n");
}
}
}``````

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

``````#include<stdio.h>

int ConsecutiveNumberInArray(int arr[10])
{
int i,j,k;
int count = 0;
int trac = 0;
int num;
int arr_val;
int index_chk = 0;

printf("Enter the array elements: ");
for(i=0; i<7; i++)
{
scanf("%d", &arr_val);
arr[i] = arr_val;
}

printf("The array is: ");
for(j=0; j<7; j++)
{
printf("%d  ", arr[j]);
}

printf("\n\nEnter the number to searched for consecutively: ");
scanf("%d", &num);

for(k=0; k<7; k++)
{
if((num == arr[k]) && (k == trac))
{
count++;
++trac;
printf("count = %d and trac = %d\n", count , trac);
if(count ==3)
break;
}
else
{
count = 0;
trac++;
}
}
if(count == 3)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int result;
int array[10];

result = ConsecutiveNumberInArray(array);

if(result == 1)
{
printf("Searched number appeared consecutively\n");
}
else
{
if(result == 0)
{
printf("Searched number does not appeared consucutively\n");
}
}``````

}

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

package amazon;

/**
* Write a code to return a value 'True' if the number '1' throughout the array appears consecutively. Ex: S = {1,1,1,0,0,3,4}.
* Else, return 'False' if the array does not have the given number (char = '1' in this case) in the consecutive order. Ex: S = {1,1,0,0,1,3,4}
*/
public class ConsecutiveNumbers {
public static void main(String ...args) {
// Test case #1
int[] arrTrue = new int[] {1,1,1,0,0,3,4}, arrFalse = new int[] {1,1,0,0,1,3,4};
int num = 1;

System.out.println(hasConsecutive(arrTrue, num));
System.out.println(hasConsecutive(arrFalse, num));

// Test case #1
int[] arrTest = new int[] {2, 6, 4, 10, 10, 20, 12, 10};
int num10 = 10;

System.out.println(hasConsecutive(arrTest, num10));
}

public static boolean hasConsecutive(int[] arr, int num) {
boolean found = false, foundAgain = false, lost = false;
for (int i = 0; i < arr.length; i++) {
if(arr[i] == num) {
found = true;
if(lost) {
foundAgain = true;
}
} else {
if(found) {
lost = true;
}
}
}

return found && !foundAgain;
}
}

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

``````package amazon;

/**
* Write a code to return a value 'True' if the number '1' throughout the array appears consecutively. Ex: S = {1,1,1,0,0,3,4}.
* Else, return 'False' if the array does not have the given number (char = '1' in this case) in the consecutive order. Ex: S = {1,1,0,0,1,3,4}
*/
public class ConsecutiveNumbers {
public static void main(String ...args) {
// Test case #1
int[] arrTrue = new int[] {1,1,1,0,0,3,4}, arrFalse = new int[] {1,1,0,0,1,3,4};
int num = 1;

System.out.println(hasConsecutive(arrTrue, num));
System.out.println(hasConsecutive(arrFalse, num));

// Test case #1
int[] arrTest = new int[] {2, 6, 4, 10, 10, 20, 12, 10};
int num10 = 10;

System.out.println(hasConsecutive(arrTest, num10));
}

public static boolean hasConsecutive(int[] arr, int num) {
boolean found = false, foundAgain = false, lost = false;
for (int i = 0; i < arr.length; i++) {
if(arr[i] == num) {
found = true;
if(lost) {
foundAgain = true;
}
} else {
if(found) {
lost = true;
}
}
}

return found && !foundAgain;
}
}``````

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

``````const array =  [1,1,1,0,0,3,4,1];

function isConsecutiveOne(array){
let firstOccurance = -1;
let lastOccurance = -1;
let totalOccurance = 0;

for(let i = 0; i < array.length; i++){
if(array[i] === 1){
if(firstOccurance === -1){
firstOccurance = i;
}else{
lastOccurance = i;
}
totalOccurance += 1;
}
}
return (lastOccurance - firstOccurance) + 1 === totalOccurance;
}

isConsecutiveOne(array);``````

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

#s = [1,1,1,0,0,3,4]
s= [1,1,0,0,1,3,4]

index = s.index(1)
print(index)

for i in range(index+1,len(s)):
if s[i]==1:
continue
else:
s = s[i:]
break

if len(s)==0:
print('True')
try:
if s.index(1):
print('False')
except ValueError:
print('True')

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

done

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

``````public static boolean verifyConsecutiveValue(int[] inputArr, int key) {

boolean flag = false;

for (int i = 0; i < inputArr.length - 1; i++) {
if (inputArr[i] == inputArr[i + 1] && inputArr[i]== key) {
flag = true;
}
}
return flag;
}``````

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

const b = [1,1,9,7,1,0,0]; //false

let foundOne = false;
let endOne = false;
let conseOne = true;
for (let i =0; i < b.length; i++) {
if(b[i] == 1 && foundOne == false) {// found first 1
foundOne = true;
}
if(b[i] != 1 && foundOne) { // end of consecutive 1
endOne = true;
}
if(endOne && b[i] == 1)
conseOne = false; // dont expect more 1
}

console.log(conseOne);

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

{{bool isConsecutive(int[] arr, int num)
{
var lastSeenIdx = -1;
for (int i = 0; i < arr.Length; ++i)
{
if (arr[i] == num)
{
if (lastSeenIdx != -1 && (i - lastSeenIdx > 1))
return false;
lastSeenIdx = i;
}
}
return lastSeenIdx != -1;
}}}

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

import java.util.*;
import java.io.*;

public class MyClass {
public static void main(String args[]) {
int[] arr = {2,2,1,1,0,1,0,3,4};
int startingPositionOfOne = 0;
int currentCounter = 0;
boolean isSequential = false;

while(arr[startingPositionOfOne]!=1) {
startingPositionOfOne++;
}

for(int i=startingPositionOfOne; i<arr.length; i++) {
if(i<(arr.length-1) && arr[i]==arr[i+1]) isSequential=true;
else {
currentCounter = i;
break;
}
}

for(int i=currentCounter+1; i<arr.length; i++) {
if(i<(arr.length-1) && arr[i]==1) isSequential = false;
}

System.out.print(isSequential);
}
}

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

``````import java.util.*;
import java.io.*;

public class MyClass {
public static void main(String args[]) {
int[] arr = {2,2,1,1,0,1,0,3,4};
int startingPositionOfOne = 0;
int currentCounter = 0;
boolean isSequential = false;

while(arr[startingPositionOfOne]!=1) {
startingPositionOfOne++;
}

for(int i=startingPositionOfOne; i<arr.length; i++) {
if(i<(arr.length-1) && arr[i]==arr[i+1]) isSequential=true;
else {
currentCounter = i;
break;
}
}

for(int i=currentCounter+1; i<arr.length; i++) {
if(i<(arr.length-1) && arr[i]==1) isSequential = false;
}

System.out.print(isSequential);
}``````

}

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

def checkConsecutive(arr, target):
lastKnownIndex = {}
for i,num in enumerate(arr):
if num == target:
lastKnownIndex[i]=num
if lastKnownIndex is not None:
indexList = []
for keys in lastKnownIndex.keys():
indexList.append(keys)
if len(indexList)-(indexList[-1]-indexList[0]) == 1:
return True

return False

if __name__ =="__main__":
arr = [1,1,1,0,3,11,4,4,2,4]
target = 1
print(checkConsecutive(arr, target))

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

``````def checkConsecutive(arr, target):
lastKnownIndex = {}
for i,num in enumerate(arr):
if num == target:
lastKnownIndex[i]=num
if lastKnownIndex is not None:
indexList = []
for keys in lastKnownIndex.keys():
indexList.append(keys)
if len(indexList)-(indexList[-1]-indexList[0]) == 1:
return True

return False

if __name__ =="__main__":
arr = [1,1,1,0,3,11,4,4,2,4]
target = 1
print(checkConsecutive(arr, target))``````

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

int[] arr={1, 1, 1, 0, -10, -9, 0, 8, 1};
List<Integer> list=new ArrayList<>();
for(Integer num:arr)
{
}
int l=list.indexOf(1);
int r=list.lastIndexOf(1);
while(l<=r)
{
if(list.get(l)!=1 || list.get(r)!=1)
{
return False;
}
else {
l++;r--;
}
}
return True;

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

``````int[] arr={1, 1, 1, 0, -10, -9, 0, 8, 1};
List<Integer> list=new ArrayList<>();
for(Integer num:arr)
{
}
int l=list.indexOf(1);
int r=list.lastIndexOf(1);
while(l<=r)
{
if(list.get(l)!=1 || list.get(r)!=1)
{
return False;
}
else {
l++;r--;
}
}
return True;``````

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.