Amazon Interview Question
Testing / Quality AssurancesCountry: India
Interview Type: In-Person
/* 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));
}
}
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));
}
}
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));
}
}
/* 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));
}
}
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");
}
}
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");
}
}
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");
}
}
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
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]);
}
}
}
- Satvik Vishnubhatta December 27, 2016