Microsoft Interview Question for Software Engineer in Tests






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

so if you have 5 4 3 2 1 of size 5, that will also return -1?

- Anonymous June 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about a binary search?

bool findInflex(int a[], int n)
{
	int lo = 0;
	int hi = n-1;
	bool firstHalfIncreasing = (a[1] - a[0])>0 ?
	bool secondHalfIncreasing = (a[n-1] - a[n-2]) > 0?

	while(1)
	{
		mid = (lo + hi)/2;
		
		if( (a[mid-1] - a[mid]) * (a[mid] - a[mid+1]) < 0)
			return mid;
		else
		{
			if(lo == hi) //we are stuck
				return -1;

			if(firstHalfIncreasing && !secondHalfIncreasing && (a[mid] - a[mid-1] )>0 ||
			   !firstHalfIncreasing && secondHalfIncreasing && (a[mid] - a[mid-1]) <0 ) //this is first half
				lo = mid;
			else
				hi = mid;
		}
	}
}

- mystique June 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The previous code doesn't compile. Sorry about that. Here goes it again:

int findInflex(int a[], int n)
{
	int lo = 0;
	int hi = n-1;
	bool firstHalfIncreasing = (a[1] - a[0])>0 ? true: false ;
	bool secondHalfIncreasing = (a[n-1] - a[n-2]) > 0? true: false;

	while(1)
	{
		int mid = (lo + hi)/2;
		
		if( (a[mid-1] - a[mid]) * (a[mid] - a[mid+1]) < 0)
			return mid;
		else
		{
			if(lo == hi) //we are stuck
				return -1;

			if(firstHalfIncreasing && !secondHalfIncreasing && (a[mid] - a[mid-1] )>0 ||
			   !firstHalfIncreasing && secondHalfIncreasing && (a[mid] - a[mid-1]) <0 ) //this is first half
				lo = mid;
			else
				hi = mid;
		}
	}
}

- mystique June 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Modifying the question:
Even in case where both parts of array are sorted in ascending, there can be point of inflexion.
Eg: 1 2 3 4 1 2 3 4 5

Here point of inflexion is 4 though both parts 1 2 3 4 and 1 2 3 4 5 are sorted in ascending.

For creating AVL trees etc, space complexity is needed which is unacceptable.

- Modified question : June 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The simplest way is tree.
Step1. Create a tree of sequence if it's a left/right skew tree then the list is either in ascending/descending order.
Step2. Then by height balancing tree algo (AVL/RBTree) we can find the index.

- kash_1 June 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

simple solution in time O(n) space O(1) - by comparing elements...
idea is, first 2 elements will always decide direction and then onwards comparison can be used to find change....

public static int findInflexion(int[] d) {
		int i=1;
		boolean fwd=true;
		
		if( d.length < 3 ) return -1;
		if(d[1]< d[0]) fwd=false;
		
		if(fwd) {
			for(int j=2;j<d.length;j++) {			
				if(d[j]>=d[j-1])
					i=j; // still increasing
				else {
					if(d[j+1]>=d[j])return -1;
					else return i;
				}
			}
		}
		else {
			for(int j=2;j<d.length;j++) {			
				if(d[j]<=d[j-1])
					i=j; // still increasing
				else {
					if(d[j+1]<=d[j])return -1;
					else return i;
				}
			}
		}
		return i;
	}

- phm June 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this code has a bug for a special condition, but that can be resolved easily.... find it and solve it :)

- phm June 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int a[] = new int[]{1,1,2,3,4,2,1,1,1};
		Boolean increasing = null;
		int last = a[0];
		for (int i = 1; i < a.length; i++) {
			if (increasing == null) {
				if (a[i] > last)
					increasing = true;
				else if (a[i] < last)
					increasing = false;
				last = a[i];
				continue;
			}
			if ((increasing && a[i] < last) || (!increasing && a[i] > last)) {
				System.out.println(i + " " + a[i]);
				return;
			}
			last = a[i];
		}
		System.out.println("-1");

- ank June 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Get the difference between two elements. Decreasing is - while increasing is +. Then check the changing point.
for example 94512678, we get 1459 8762. Then we have +++ ----, therefore, the inflexion point is 4.

- chenming831 June 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

chenming831 i could not get ur example. can u elaborate it a bit

- Anonymous June 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] a = { 1, 1, 2, 3, 4, 2, 1, 1, 1 };
int FindArrayInflexionIndex(int[] a)
{

for (int i = 1; i < a.Length - 1; i++)
{
if ((a[i] > a[i + 1]) != (a[0] > a[1]))
return i;
}

return -1;
}

- Anonymous June 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dosen't work with: { 7, 7, 6, 5, 4, 5, 6, 7 }

- Anonymous June 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dosen't work with: { 7, 7, 6, 5, 4, 5, 6, 7 }

- Anonymous June 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int FindInflectionPoint(int[] a)
{            
  if (a.Length > 1)
  {
    for (int i = 1; i < a.Length - 1; i++)
    {
      if ( ((a[i] - a[i - 1]) * (a[i + 1] - a[i])) < 0)
        return i;
    }
  }

  return -1;
}

- Anonymous June 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can it be done in less than O(n)???

- Anonymous July 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think you mean peak or trough. "Inflection point" means something completely different.

- Anon July 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what is the inflexion point for 9 8 7 6 5 6 7 8 ?

- Anonymous August 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For the above, as I understand inflexion point is at index 5.
static int findflip(int[] a)
{
bool? Firstinc = null;

for (int i = 0; i < a.Length - 1; i++)
{
if (a[i] < a[i + 1])
{
if (Firstinc == false) return i+1;
Firstinc = true;
}
else if (a[i] > a[i + 1])
{
if (Firstinc == true) return i+1;
Firstinc = false;
}
}
return -1;
}

- c# August 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int inflexion(int a[], int n)
{
if(n<3)
return -1;
int i, diff = 0, old_diff=0;
old_diff = a[1] - a[0];
for(i=1; i<n-1;i++)
{
diff = a[i+1] - a[i];
if(diff>0 && old_diff<0 || diff<0 && old_diff >0)
return i;
else
old_diff = diff;
}
return -1;
}

- Anonymous September 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is an easy one which can be done in O(n) time and O(1) space ...
Suppose the array a is 9 8 7 6 5 7 8 9
maintain a pointer j to the beginning of the array
keep incrementing j until j+2 reaches the end of the array .
when ever signs of a[j]-a[j+1] and a[j+1]-a[j+2] is different then j+1 is the point of inflexion .
so in this case 5 is the point of inflexion as
signs of 6-5 and 5-7 are different ....

- gulusworld1989 October 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int Findinflexion(int inp[], int len)
{
int check = 0;
inp[1] - inp[0] >= 0?check = 1:check = -1;

int prev = inp[0];
int index = -1;
for(int i=1;i<len;i++)
{
int tempcheck = 0;
inp[i]-prev > 0?tempcheck = 1:tempcheck = -1;

if(tempcheck != check)
{
index = i;
break;
}
prev = inp[i];
}

return index;
}

- Ashish October 30, 2010 | 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