Microsoft Interview Question


Country: -




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

Binary search.
Not tested:

int findDirectionSwitch(int[] arr)
{
    return findDirectionSwitch(arr, 0, arr.length);
}
int findDirectionSwitch(int[] arr, int startIndex, int stopIndex)
{
    if (stopIndex - startIndex < 3)
    {
        // throw error or return -1 to symbolize no answer;
        return -1;
    }

    int middleIndex = (stopIndex - startIndex) / 2 + startIndex;
    
    // Note: i could have done less than previous
    // I am technically detecting the one before the decreasing start one.
    // That is why when I find it I have to return +1. 
    bool biggerThanPrevious = arr[middleIndex] > arr[middleIndex - 1];
    bool biggerThanNext = arr[middleIndex] > arr[middleIndex + 1];

    if (biggerThanPrevious && biggerThanNext)
    {
        return middle + 1;
    }

    if (biggerThanPrevious)
    {
        // Note I didn't do middle+1 here because I like to keep 3.
        return findDirectionSwitch(arr, middle, stopIndex);
    }
    else
    {
        // Note I didn't do middle-1 here because I like to keep 3.
        return findDirectionSwitch(arr, startIndex, middle);
    }
}

- Anonymous October 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how about 10, 12, 10, 8 ?

- Anonymous October 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In this problam we can use the ternary search algorithm, because our function is unimodal
This is my simple python sol.

def solve(left, right, arr):
    '''Finds max element in an array'''

    if right - left <= 2:
        t = arr[left:right + 1]
        return left + t.index(max(t))

    # devide arr to three segments [left, m1], [m1, m2], [m2, right]
    m1 = (2 * left + right) / 3
    m2 = (left + 2 * right) / 3

    # values in arr forms unimodal function, so
    # we can assume that if arr[m1] <= arr[m2]
    # our max lies in [m1, right]
    if arr[m1] <= arr[m2]:
        return solve(m1, right, arr)
    else:
        return solve(left, m2, arr)

if __name__ == '__main__':
    arr = [10, 12, 16, 17, 24, 27, 8, 6, 5, 4, 2]
    print solve(0, len(arr), arr) + 1

Sorry for my bad English )

- V.Krestyannikov October 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

def find_max(A):
    a = 0
    b = len(A)-1
    
    while True:
        c = (a+b)/2
        if A[c]>A[c-1] and A[c]>A[c+1]:
            return A[c]
        if A[c-1]>A[c]: 
            b=c
        else: 
            a=c

A = [10, 12, 16, 17, 24, 27, 8, 6, 5, 4, 2]
print find_max(A)

- its easier than that October 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
        int arr[] = {10, 12, 16, 17, 24, 27, 8, 6, 5, 4, 2};
        
        int last = java.lang.Integer.MIN_VALUE;
        
        for (int i = 0; i < arr.length; i++)
        {
          if (arr[i] > last)
          {
            last = arr[i];
          }
          else
          {
            System.out.println("Value: " + arr[i] + " index: " + i );
            break;
          }
        }
    }

- imhungry October 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not having the complexity O(logn)

- Nani November 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
int max(int a[],int,int);
int main()
{
int a[6]={12,13,14,10,6,1};
int len;
int i;
printf("%d",max(a,0,5));
getchar();
getchar();
return 0;
}
int max(int a[],int s,int e)
{
int mid=(s+e)/2;
if(s<=e){
if(a[s]>a[s+1])return a[s];
if(a[e]>a[e-1])return a[e];
if(a[mid]>a[mid+1] && a[mid]>a[mid-1])
{
return a[mid];
}
else if(a[s]>a[mid])
{
return max(a,s,mid-1);
}
else
{
return max(a,mid+1,e);
}}
else
return -1;
}

- Nascent Coder October 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int findIncPos(int arr[], int left, int right)
{
int Lval;
int Rval;
int CurVal;
int mid = left + ((right - left) + 1)/2;
int nextLeft = left;
int nextRight = right;

if (1 == mid)
return mid;

Lval = arr[mid - 1];
Rval = arr[mid + 1];
CurVal = arr[mid];

if (Lval < CurVal)
if (Rval > CurVal)
nextLeft = mid+1;
else
return mid + 1;
else
if (Rval < CurVal)
return mid;
else
nextRight = mid - 1;

return findIncPos(arr, nextLeft, nextRight);
}


int main()
{
int array[] = {10, 3,2,1};
int rightIdx = (sizeof(array)/sizeof(array[0])) - 1;

int pos = findIncPos(array, 0, rightIdx);

printf("Postiion N Val of order change is %d, %d \r\n", pos+1, array[pos]);

}

- Anonymous October 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey42219" class="run-this">#include <stdio.h>

int findIncPos(int arr[], int left, int right)
{
int Lval;
int Rval;
int CurVal;
int mid = left + ((right - left) + 1)/2;
int nextLeft = left;
int nextRight = right;

if (1 == mid)
return mid;

Lval = arr[mid - 1];
Rval = arr[mid + 1];
CurVal = arr[mid];

if (Lval < CurVal)
if (Rval > CurVal)
nextLeft = mid+1;
else
return mid + 1;
else
if (Rval < CurVal)
return mid;
else
nextRight = mid - 1;

return findIncPos(arr, nextLeft, nextRight);
}


int main()
{
int array[] = {10, 3,2,1};
int rightIdx = (sizeof(array)/sizeof(array[0])) - 1;

int pos = findIncPos(array, 0, rightIdx);

printf("Postiion N Val of order change is %d, %d \r\n", pos+1, array[pos]);
}
</pre><pre title="CodeMonkey42219" input="yes">Please post incase this fails for ne input..</pre>

- Anonymous October 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code works fine. but need two changes.

1. Mid element need to find out accurately
eg. (right - left) / 2 + left;

2. While printing printf("Postiion N Val of order change is %d, %d \r\n", pos+1, array[pos]);

It should be pos instead of pos+1

- Sidhartha October 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. unlike only one partition, any other partition will lead to exctly one partition sorted
2. chk if sorted partition is increasing or decreasing
3. if increasing search in right partition
4 else search in left sub array(left partition)

BS(lo, hi)
	m = (lo + hi)/2
	if(a[m-1] <a[m] && a[m+1] < a[m])
		return m
	if(a[m-1] <a[m] && a[m+1] > a[m])
		BS(m+1, hi)
	if(a[m] < a[m-1] && a[m] > a[m+1])
		BS(lo, m-1)

- Prateek Caire November 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class FindRotation {
	public int findPos(int[] array) {
		if(array == null || array.length < 3) {
			throw new IllegalArgumentException("invalid array size");
		}
		int start = 0;
		int end = array.length - 1;
		while(start <= end) {
			int mid = start + (end - start) / 2;
			if(mid == 0 || mid + 1 >= array.length) {
				break;
			} else if(array[mid - 1] < array[mid] && array[mid + 1] < array[mid]) {
				return mid;
			} else if(array[mid - 1] < array[mid] && array[mid + 1] > array[mid]) {
				start = mid + 1;
			} else if(array[mid - 1] > array[mid] && array[mid + 1] < array[mid]) {
				end = mid - 1;
			} else {
				break;
			}
		}
		return -1;
	}
}

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

public class FindSplitPos {
	public int findPos(int[] array) {
		if(array == null || array.length < 3) {
			throw new IllegalArgumentException("invalid array size");
		}
		int start = 0;
		int end = array.length - 1;
		while(start <= end) {
			int mid = start + (end - start) / 2;
			if(mid == 0 || mid + 1 >= array.length) {
				break;
			} else if(array[mid - 1] < array[mid] && array[mid + 1] < array[mid]) {
				return mid;
			} else if(array[mid - 1] < array[mid] && array[mid + 1] > array[mid]) {
				start = mid + 1;
			} else if(array[mid - 1] > array[mid] && array[mid + 1] < array[mid]) {
				end = mid - 1;
			} else {
				break;
			}
		}
		return -1;
	}
}

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

public static int find(int[] data) {
		return find(data, 0, data.length - 1);
	}

	static int find(int[] data, int start, int end) {
		if (end > start) {
			int mid = start + (end - start) / 2;
			int v = data[mid];
			if (v < data[mid + 1])
				return find(data, mid + 1, end);
			else if (mid == 0)
				return 0;
			else if (data[mid - 1] > v)
				return find(data, start, mid);
			else
				return mid;
		} else
			return -1;

	}

- Anonymous October 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

why not thsi simple one :)
int ModBinaryS(int A[],int n)
{
int st,en;
int med;
st=0;
en=n-1;
while(st<en)
{
mid=(st+en)/2;
if(A[mid]>A[mid+1])
return mid;
if(A[mid]<A[mid-1])
return mid-1;
if(A[mid]>A[en])
st=mid+1;
if(A[mid]<A[en])
mid=en-1;
}
return INT_MIN;
}

- geeks October 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Essentially, if the order is log n, applying binary search way is the best.

- Victor October 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can't use bin search here.

- V.Krestyannikov October 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What I am trying to mention here is that the concept behind binary search and not the binary search algorithm

- victor October 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<stdio.h>
#include<conio.h>
int max(int a[],int,int);
int main()
{
int a[6]={12,13,14,10,6,1};
int len;
int i;
printf("%d",max(a,0,5));
getchar();
getchar();
return 0;
}
int max(int a[],int s,int e)
{
int mid=(s+e)/2;
if(s<=e){
if(a[s]>a[s+1])return a[s];
if(a[e]>a[e-1])return a[e];
if(a[mid]>a[mid+1] && a[mid]>a[mid-1])
{
return a[mid];
}
else if(a[s]>a[mid])
{
return max(a,s,mid-1);
}
else
{
return max(a,mid+1,e);
}}
else
return -1;
}

- Nascent Coder October 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why post a code without few tests? Your code fails for 1 input that i tested here: ideone.com/TjJbG

Are u seriously looking for a job... I doubt... Your code is so sloppy with conio.h, getchar()... So ugly!

- anonymous2 October 03, 2011 | 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