Hughes Systique Interview Question

In First Programming Round we need to write two programs... 1)WAP to implement a Queue using two stacks. 2)WAP to return the index position of the first occurance of '1' in the following array [0,0,0,0,0,,,,,,,,,1,1,1,,,,,,,1]

Interview Answer

Anonymous

Sep 21, 2018

public static void main(String[] args) { int arr[]= {0,0,0,0,0,0,0,1,1,1,1,0,0,0}; int n = arr.length; System.out.println(getFirstIndex(arr,0,n-1)); } public static int getFirstIndex(int array[], int low, int high) { while(low<= high) { int mid = (low+high)/2; if(array[mid]==1&&(mid==1||array[mid-1]==0)) { return mid; } else if(array[mid]==1){ high = mid-1; } else { low = mid+1; } } return -1; }

3