Yatra.com Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Modified Binary search will do

int search(int a[], int l, int u, int x) {
    while (l <= u) {
        int m = (l + u) / 2;
        if (x == a[m]) {
            return m;
        } else if (a[l] <= a[m]) {
            if (x > a[m]) {
                l = m+1;
            } else if (x >=a [l]) {
                u = m-1;
            } else {
                l = m+1;
            }
        }
        else if (x < a[m]) u = m-1;
        else if (x <= a[u]) l = m+1;
        else u = m - 1;
    }
    return -1;
}

- cooldaa January 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Incomplete question.

Is it supposed to be a sorted array which has been rotated an unknown number of times?

- Anonymous January 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

binary search + shift

- p January 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this question has som mistak i guess???

- rockstar January 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a cyclically ordered list. We can use a variant of binary search like cyclic binary search ( with little modification) to solve the problem.

- dusi January 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a cyclically ordered list. We can use a variant of binary search like cyclic binary search ( with little modification) to solve the problem.

- dusi January 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Approach:
1. First search for the index of the element which is inverted. Example in the example given array {5,6,7,8,9,10,1,2,3,4} the index is '5' (element 10). Running time (lg n) [Modified binary search]
2. Do an ordinary binary search in the appropriate sub-array.

/*
     * Return the index of the highest element in the array
     */
    public int searchForBoundry(int[] arr, int start, int end) {
    	if (start >= end) {
    		return -1;
    	}   	
    	int mid = start + (end-start)/2;
    	
    	if (arr[mid] > arr[mid+1]) {
    		return mid;
    	}
    	if (arr[start] < arr[mid]) {
    		return searchForBoundry(arr, mid, end);
    	} else {
    		return searchForBoundry(arr, start, mid);
    	}
    }
    
    /*
     * Ordinary Binary Search
     */
    public int binarySearch(int[] arr, int start, int end, int element) {
    	if (start > end) {
    		return -1;
    	}
    	
    	int mid = start + (end-start)/2;
    	
    	if (arr[mid] > element) {
    		return binarySearch(arr, start, mid-1, element);
    	} else if (arr[mid] < element) {
    		return binarySearch(arr, mid+1, end, element);    		
    	}else {
    		return mid;
    	}
    }
    
    public int searchCircular(int[] arr, int start, int end, int element) {
    	int index = searchForBoundry(arr, start, end);
    	if (index != -1) {
    		if (arr[start] > element) {
    			return binarySearch(arr, index+1, end, element);
    		} else {
    			return binarySearch(arr, start, index, element);
    		}
    	} else {
    		return -1;
    	}
    }

- successboy123 September 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Code for searchForBoundry without recursion

public int searchForBoundry(int[] arr) {
    	int start=0;
		int end=arr.length;
		int mid;
		
		if (end<=1) {
    		return (end-1);
    	}

		while(start < end){
			mid = (start + end)/2;
			
			if (arr[mid] > arr[mid+1]) 
				return mid;
			
			if (arr[start] < arr[mid]) {
				start = mid;
			} else {
				end = mid;
			}
		}
		return -1;
    }

- Suman October 17, 2013 | Flag


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