Amazon Interview Question for Testing / Quality Assurances


Country: India
Interview Type: In-Person




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

/* Binary Search Algorithm: The algorithm actually works with a sorted array. Taking the middle element of the array or a list,and comparing it with a searching element if it is less than it, the lower half of the array is halved again, else upper half is halved. It goes on till it the middle element becomes the searching element.*/

class BinarySearchAlgorithm{

   static int search_method(int[] sorted_array,int low_index, int high_index, int search_elem){
        int mid_index=(low_index+(high_index-1))/2;
    if(low_index < high_index){
       /* Searching in the lower half*/
        if ( search_elem < sorted_array[mid_index])           
             return search_method(sorted_array,low_index,mid_index,search_elem);
        /*Checking if the middle element*/
        if (search_elem == sorted_array[mid_index])            
             return mid_index;
        /*searching in upper half*/
        return search_method(sorted_array,mid_index+1,high_index,search_elem);
      }
     return -1; // If not found 
    
    }
  public static void main(String[] args){
         
		 int[] sort_Array={1,2,3,4,5};
         int search_element=4;
         System.out.println("Index of the search element is "+search_method(sort_Array,0,sort_Array.length,search_element));
   }
}

- Satvik Vishnubhatta December 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* Binary Search Algorithm: The algorithm actually works with a sorted array. Taking the middle element of the array or a list,and comparing it with a searching element if it is less than it, the lower half of the array is halved again, else upper half is halved. It goes on till it the middle element becomes the searching element.*/

class BinarySearchAlgorithm{

   static int search_method(int[] sorted_array,int low_index, int high_index, int search_elem){
        int mid_index=(low_index+(high_index-1))/2;
    if(low_index < high_index){
       /* Searching in the lower half*/
        if ( search_elem < sorted_array[mid_index])           
             return search_method(sorted_array,low_index,mid_index,search_elem);
        /*Checking if the middle element*/
        if (search_elem == sorted_array[mid_index])            
             return mid_index;
        /*searching in upper half*/
        return search_method(sorted_array,mid_index+1,high_index,search_elem);
      }
     return -1; // If not found 
    
    }
  public static void main(String[] args){
         
		 int[] sort_Array={1,2,3,4,5};
         int search_element=4;
         System.out.println("Index of the search element is "+search_method(sort_Array,0,sort_Array.length,search_element));
   }
}

- Satvik Vishnubhatta December 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class BinarySearchAlgorithm{

   static int search_method(int[] sorted_array,int low_index, int high_index, int search_elem){
        int mid_index=(low_index+(high_index-1))/2;
    if(low_index < high_index){
       /* Searching in the lower half*/
        if ( search_elem < sorted_array[mid_index])           
             return search_method(sorted_array,low_index,mid_index,search_elem);
        /*Checking if the middle element*/
        if (search_elem == sorted_array[mid_index])            
             return mid_index;
        /*searching in upper half*/
        return search_method(sorted_array,mid_index+1,high_index,search_elem);
      }
     return -1; // If not found 
    
    }
  public static void main(String[] args){
         
		 int[] sort_Array={1,2,3,4,5};
         int search_element=4;
         System.out.println("Index of the search element is "+search_method(sort_Array,0,sort_Array.length,search_element));
   }
}

- Anonymous December 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class BinarySearchAlgorithm{

   static int search_method(int[] sorted_array,int low_index, int high_index, int search_elem){
        int mid_index=(low_index+(high_index-1))/2;
    if(low_index < high_index){
       /* Searching in the lower half*/
        if ( search_elem < sorted_array[mid_index])           
             return search_method(sorted_array,low_index,mid_index,search_elem);
        /*Checking if the middle element*/
        if (search_elem == sorted_array[mid_index])            
             return mid_index;
        /*searching in upper half*/
        return search_method(sorted_array,mid_index+1,high_index,search_elem);
      }
     return -1; // If not found 
    
    }
  public static void main(String[] args){
         
		 int[] sort_Array={1,2,3,4,5};
         int search_element=4;
         System.out.println("Index of the search element is "+search_method(sort_Array,0,sort_Array.length,search_element));
   }
}

- Anonymous December 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* Binary Search Algorithm: The algorithm actually works with a sorted array. Taking the middle element of the array or a list,and comparing it with a searching element if it is less than it, the lower half of the array is halved again, else upper half is halved. It goes on till it the middle element becomes the searching element.*/

class BinarySearchAlgorithm{

   static int search_method(int[] sorted_array,int low_index, int high_index, int search_elem){
        int mid_index=(low_index+(high_index-1))/2;
    if(low_index < high_index){
       /* Searching in the lower half*/
        if ( search_elem < sorted_array[mid_index])           
             return search_method(sorted_array,low_index,mid_index,search_elem);
        /*Checking if the middle element*/
        if (search_elem == sorted_array[mid_index])            
             return mid_index;
        /*searching in upper half*/
        return search_method(sorted_array,mid_index+1,high_index,search_elem);
      }
     return -1; // If not found 
    
    }
  public static void main(String[] args){
         
		 int[] sort_Array={1,2,3,4,5};
         int search_element=4;
         System.out.println("Index of the search element is "+search_method(sort_Array,0,sort_Array.length,search_element));
   }
}

- Satvik Vishnubhatta December 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BinarySearch1
{
	public static void main(String[] args)
	{
		int[] array={23,43,21,87,98,67,56,80,76,55};
		int search=76;
		Arrays.sort(array);
		for(int i=0;i<=array.length-1;i++)
		{
			System.out.print(array[i]+",");
		}
		int start=0;
		int end=array.length-1;
		int mid=(start+end)/2;
		System.out.println();
		
		while(start<=end)
		{
			if(search==array[mid])
			{
				System.out.println("Number found at index "+mid);
				break;
			}
			else if(search>array[mid])
			{
				start=mid+1;
			}
			else if(search<array[mid])
			{
				end=mid-1;
			}
			mid=(start+end)/2;
		}
		if(start>end)
		{
			System.out.println("Number not found");
		}	
	}

- Anonymous December 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BinarySearch1
{
	public static void main(String[] args)
	{
		int[] array={23,43,21,87,98,67,56,80,76,55};
		int search=76;
		Arrays.sort(array);
		for(int i=0;i<=array.length-1;i++)
		{
			System.out.print(array[i]+",");
		}
		int start=0;
		int end=array.length-1;
		int mid=(start+end)/2;
		System.out.println();
		
		while(start<=end)
		{
			if(search==array[mid])
			{
				System.out.println("Number found at index "+mid);
				break;
			}
			else if(search>array[mid])
			{
				start=mid+1;
			}
			else if(search<array[mid])
			{
				end=mid-1;
			}
			mid=(start+end)/2;
		}
		if(start>end)
		{
			System.out.println("Number not found");
		}	
	}

- Alok December 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;
class binarysearch{
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
int c, first, last, middle, n, search;
int array[]=new int[100];

System.out.println("Enter number of elements");
n=scan.nextInt();
for (c = 0; c < n; c++)
array[c]=scan.nextInt();

System.out.println("Enter value to find\n");
search=scan.nextInt();

first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
System.out.println(search+"found at location "+(middle+1));
break;
}
else
last = middle - 1;

middle = (first + last)/2;
}
if (first > last)
System.out.println("Not found! "+search+" is not present in the list");

}
}

- lavakumarfire999 December 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

module.exports = function (haystack, needle) {
  var i, j, m
  i = 0
  j = haystack.length - 1
  m = Math.floor(haystack.length / 2)
  while (i <= j) {
    if (haystack[m] === needle) {
      return true
    }
    if (haystack[m] < needle) {
      i = m + 1
    } else {
      j = m - 1
    }
    m = i + Math.floor((j - i + 1) / 2)
  }
  return false
}

var A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for (var i = 0; i < 12; ++i) {
  var bool = module.exports(A, i)
  console.log('Searching for `' + i + '`, .... ' + bool)
}

// $ node binary-search.js 
// Searching for `0`, .... false
// Searching for `1`, .... true
// Searching for `2`, .... true
// Searching for `3`, .... true
// Searching for `4`, .... true
// Searching for `5`, .... true
// Searching for `6`, .... true
// Searching for `7`, .... true
// Searching for `8`, .... true
// Searching for `9`, .... true
// Searching for `10`, .... true
// Searching for `11`, .... false

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

public class BinarySearch {

    public int sort(int searchItem, int[] sortedArray, int lower, int upper) {
        int mid = lower + (upper - lower) / 2;
        if (sortedArray[mid] == searchItem)
            return searchItem;
        else if (sortedArray[mid] > searchItem) {
            return sort(searchItem, sortedArray, lower, mid - 1);
        } else {
            return sort(searchItem, sortedArray, mid + 1, upper);
        }
    }

    public static void main(String[] args) {
        int[] arr = {10, 5, 6, 9, 2, 1, 102, 56, 2, 1};
        BinarySearch bs = new BinarySearch();
        BubbleSort sort = new BubbleSort();
        int[] sortedArray = sort.sort(arr);
        int len = sortedArray.length;
        int lower = 0;
        int upper = len - 1;
        int mid = (upper - lower) / 2;
        bs.sort(10, sortedArray, lower, upper);

        for (int i = 0; i < sortedArray.length; i++) {
            System.out.println(sortedArray[i]);
        }

    }
}

- ajnavi July 31, 2018 | 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