## Amazon Interview Question for Quality Assurance Engineers

• 1
of 1 vote

Country: India

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

``````def solve(arr):
left=0
right=len(arr)-1
while left<right:
while left<right and arr[left]==0:
left+=1
while left<right and arr[right]==1:
right-=1
arr[left],arr[right]=arr[right],arr[left]
left+=1
right-=1
return arr``````

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

O(n) where in is the length of bin_array
In python:

``````def sort(bin_array):
length = len(bin_array)
ones = sum(bin_array)
zeros = length - ones
return [0 for _ in range(zeros)] + [1 for _ in range(ones)]``````

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

a simple bubble sort will work
for(int i=0; i<arr.length;i++) {
if(arr[i] == 0) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j++;
}
}

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

Let me give you an O(2n) algorithm (pseudocode).

``````sum = 0;
binaryArray = [0, 1, 1, 0, 0, 1, 0, 0, 1];

for i in binaryArray:
sum = sum + i;

length = len(binaryArray)
result = zeros(length)

for i, value in enumerate(result):
if sum > 0:
result[length - i] = 1
sum = sum - 1``````

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

``````public static int[] zerosToLeft(int[] arr) {
int j = arr.length-1;
int[] rez = new int[arr.length];
for(int i = 0; i < arr.length; i++){
if(arr[i] == 1) {
rez[j] = arr[i];
j--;
}
}
return rez;``````

}

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

let a = [1,0,1,0,0,1,0,1,0,0,0,0,0]

func zeroFollowedByOne(a: [Int]) -> [Int] {
var a = a
var l = 0
var h = a.count - 1

while l < h {
if a[l] == 1 && a[h] == 0 {
let temp = a[h]
a[h] = a[l]
a[l] = temp
l+=1
h-=1
} else if a[l] == 0 && a[h] == 1 {
l+=1
h-=1
} else if a[l] == 0 && a[h] == 0 {
l+=1
}
}

return a
}

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

let a = [1,0,1,0,0,1,0,1,0,0,0,0,0]

``````func zeroFollowedByOne(a: [Int]) -> [Int] {
var a = a
var l = 0
var h = a.count - 1

while l < h {
if a[l] == 1 && a[h] == 0 {
let temp = a[h]
a[h] = a[l]
a[l] = temp
l+=1
h-=1
} else if a[l] == 0 && a[h] == 1 {
l+=1
h-=1
} else if a[l] == 0 && a[h] == 0 {
l+=1
}
}

return a
}``````

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

let a = [1,0,1,0,0,1,0,1,0,0,0,0,0]

``````func zeroFollowedByOne(a: [Int]) -> [Int] {
var a = a
var l = 0
var h = a.count - 1

while l < h {
if a[l] == 1 && a[h] == 0 {
let temp = a[h]
a[h] = a[l]
a[l] = temp
l+=1
h-=1
} else if a[l] == 0 && a[h] == 1 {
l+=1
h-=1
} else if a[l] == 0 && a[h] == 0 {
l+=1
}
}

return a``````

}

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

public static void main(String[] args) {

int array[]= {0,1,1,0,0,1,0,0,1};
int lastIndexValue=array.length-1;
int newArray[]= new int[array.length];

for(int i=0;i<array.length;i++) {

if(array[i]==1) {
newArray[lastIndexValue]=1;
lastIndexValue--;
}
}

System.out.println(Arrays.toString(newArray));
}

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

``````public static void main(String[] args) {

int array[]= {0,1,1,0,0,1,0,0,1};
int lastIndexValue=array.length-1;
int newArray[]= new int[array.length];

for(int i=0;i<array.length;i++) {

if(array[i]==1) {
newArray[lastIndexValue]=1;
lastIndexValue--;
}
}

System.out.println(Arrays.toString(newArray));
}``````

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

``````def sortbits(il):

for i in range(len(il)):
if il[i] == 1:
for j in range(len(il) - 1, i + 1, -1):
if il[j] == 0:
il[i], il[j] = il[j], il[i]
break

return il

driver = [0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0]
print(sortbits(driver))``````

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

``````def sortbits(il):
for i in range(len(il)):
if il[i] == 1:
for j in range(len(il) - 1, i + 1, -1):
if il[j] == 0:
il[i], il[j] = il[j], il[i]
break
return il

driver = [0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0]
print(sortbits(driver))``````

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

``````int arr[]= {0,1,1,0,0,1,0,0,1};
int j = 0;
for(int i=0; i<arr.length;i++) {
if(arr[i] == 0) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j++;
}
}``````

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

``````int arr[]= {0,1,1,0,0,1,0,0,1};
int j = 0;
for(int i=0; i<arr.length;i++) {
if(arr[i] == 0) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j++;
}
}``````

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

``````int arr[]= {0,1,1,0,0,1,0,0,1};
int j = 0;
for(int i=0; i<arr.length;i++) {
if(arr[i] == 0) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j++;
}``````

}

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

int arr[]= {0,1,1,0,0,1,0,0,1};
int j = 0;
for(int i=0; i<arr.length;i++) {
if(arr[i] == 0) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j++;
}
}

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

int arr[]= {0,1,1,0,0,1,0,0,1};
int j = 0;
for(int i=0; i<arr.length;i++) {
if(arr[i] == 0) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j++;
}
}

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

``````int arr[]= {0,1,1,0,0,1,0,0,1};
int j = 0;
for(int i=0; i<arr.length;i++) {
if(arr[i] == 0) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j++;
}
}``````

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

quicksort finding pivot logic

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

``````public static void main(String[] args)
{
int arr[] = { 1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9
int n = arr.length;
int count = 0;
for (int i = 0; i<=arr.length-1; i++)
{
if (arr[i] != 0)
arr[count++] = arr[i];
}
while (count < n)
{
arr[count++] = 0;
}

System.out.println("Array after pushing zeros to the back: ");
for (int i = 0; i<=arr.length-1; i++)
{
System.out.print(arr[i]+" ");
}
}``````

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

Simple sorting is fine.

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

``````public class BinaryArray {
public static void main(String[] args) {
int[] arr = new int[]{0, 0, 0, 1, 1, 1, 0};
sort(arr);
for (int a : arr) System.out.println(a);
}

private static void sort(int[] arr) {
int i = 0;
int j = arr.length - 1;
while (i <= j) {
while (arr[i] != 1) i++;

while (arr[j] != 0) j--;

if (i <= j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
}``````

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

``````public static int[] SortBinaryArr(int[]  num)
{
int len = num.Length-1;
int beg = 0;
int[] result = new int[num.Count()];

while(beg != len)
{
if(num[beg] > num[len])
{
result[beg] = num[len];
result[len] = num[beg];
}
else
{
result[beg] = num[beg];
result[len] = num[len];
}
beg++;
len--;
}
return result;
}``````

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

from array import array

array_x = array('b',[0, 1, 1, 0, 0, 1, 0, 0, 1])

k = list(array_x)
k.sort()

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

``````from array import array

array_x = array('b',[0, 1, 1, 0, 0, 1, 0, 0, 1])

k = list(array_x)
k.sort()``````

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

int[] numbers = {0,1,1,0,0,1,0,0,1};

Arrays.sort(numbers);

for(Integer n : numbers)
{
System.out.print(n);

}

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

``````int[] numbers = {0,1,1,0,0,1,0,0,1};

Arrays.sort(numbers);

for(Integer n : numbers)
{
System.out.print(n);

}``````

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

``````def sorybinarray(arr):
count = 0
for i in range(len(arr)):
if arr[i] == 0:
count = count + 1
for i in range(0, count):
arr[i] = 0
for i in range(count, len(arr)):
arr[i] = 1

arr = [0, 1, 1, 0, 0, 1, 0, 0, 1]
sorybinarray(arr)
for i in range(len(arr)):
print(arr[i])``````

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

public static int[] swapZerosOnes(int[] arr) {

int []k = new int[arr.length];
int left = 0; int right = k.length-1;

for(int i= 0 ; i<= arr.length -1 ; i++) {
if(arr[i] == 0) {
k[left] = arr[i];
left++;
}else if(arr[i] == 1) {
k[right] = arr[i];
right--;
}
}

return k;

}

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

public static int[] swapZerosOnes(int[] arr) {

int []k = new int[arr.length];
int left = 0; int right = k.length-1;

for(int i= 0 ; i<= arr.length -1 ; i++) {
if(arr[i] == 0) {
k[left] = arr[i];
left++;
}else if(arr[i] == 1) {
k[right] = arr[i];
right--;
}
}
return k;
}

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

``````public static void main(String[] args) {

int array[] = { 2,3,3,3,3,2,2, 2, 3,1,9 };

int array_len = array.length;

Arrays.sort(array);// [1, 1, 1, 1, 2, 2, 2, 2, 2]

System.out.println(Arrays.toString(array));

int newArr[] = new int[array_len];// new int[8]

for (int i = 0; i < array.length; i++) {// 0-8//9 iterations

newArr[i] = array[array_len-1];// newArr[0] = array[8]

array_len--;
}
System.out.println(Arrays.toString(newArr));``````

}

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

#Python

array = [0, 1, 1, 0, 0, 1, 0, 0, 1]
new_array = []

def sort(array):
new_array.extend([0] * array.count(0))
new_array.extend([1] * array.count(1))
print(new_array)
return

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

``````#Python
array = [0, 1, 1, 0, 0, 1, 0, 0, 1]
new_array = []

def sort(array):
new_array.extend([0] * array.count(0))
new_array.extend([1] * array.count(1))
print(new_array)
return``````

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

``````private fun sortBArray(input: Array<Int>) {
var index = 0
var zeroIndex = input.size - 1

while (index < zeroIndex) {
val item = input[index]
if (item == 1) {
index++
continue
}

val switchWith = input[zeroIndex]
input[index] = switchWith
input[zeroIndex] = item
zeroIndex--
}
}``````

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

``````fun sortBArray(input: Array<Int>) {
var index = 0
var zeroIndex = input.size - 1

while (index < zeroIndex) {
val item = input[index]
if (item == 1) {
index++
continue
}

val switchWith = input[zeroIndex]
input[index] = switchWith
input[zeroIndex] = item
zeroIndex--
}``````

}

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

``````private fun sortBArray(input: Array<Int>) {
var index = 0
var zeroIndex = input.size - 1

while (index < zeroIndex) {
val item = input[index]
if (item == 1) {
index++
continue
}

val switchWith = input[zeroIndex]
input[index] = switchWith
input[zeroIndex] = item
zeroIndex--
}
}``````

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.

### 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.

### 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.