Amazon Interview Question
Quality Assurance EngineersCountry: India
Interview Type: Written Test
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");
}
}
}
}
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");
}
}
}
}
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;
}
}
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;
}
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;
}
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;
}
#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");
}
}
}
#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");
}
}
}
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;
}
}
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;
}
}
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);
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);
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);
}
}
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);
}
}
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))
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))
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.
- swapnilkant11 June 05, 2019Implementation 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.