Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
6
of 10 vote

This can be done in O(n) using a deque whose front will always hold maximum in current window. Space Complexity - O(k)
------------------------------------------------------------------------------------------------------------

void maxInWindow(int* arr, int k)
{
      std::deque<int> Qi(k);
      int i;
      for(i = 0; i < k; i++)
      {
            while(!Qi.empty() && arr[i] >= arr[Qi.back()])
            {
                    Qi.pop_back();
            }
            Qi.push_back(i);
      }

      for( ; i < length; i++)
      {
              std::cout << arr[Qi.front()] << std::endl;
              while(!Qi.empty() && i - k >= Qi.front())
              {
                     Qi.pop_front();
               }

               while(!Qi.empty() && arr[i] >= arr[Qi.back()])
               {
                    Qi.pop_back();
               }
               Qi.push_back(i);
      }
     std::cout << arr[Qi.front()] << std::endl;
}

- Nitin Gupta March 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Why my answer is downvoted?

- Nitin Gupta March 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

please explain your code ....

- goelrinku90 March 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I do not think that there is any complex thing to explain in my code.
I am using a deque whose front will always contain maximum element.
First I am putting first K element's index in my deque along with the condition such that, If coming element is less than last element of deque then there is no need to store its index.

Then in second while loop, I am checking if current maximum goes out of the window or not.

Third while loop is similar to first one.

I don't know what is difficult to understand in this code. People wants everything to be served to them ready to eat. Please apply your mind to understand the code by running over some example. This is how you will grow.

Downvoting just for fun is not good for such a great platform provided to us.

- Nitin Gupta March 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi nitin,

Can you please explain how the time complexity is O(n) ?

- DashDash March 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ok.

If you look closely then you can see that, we are adding and removing each element in the deque, exactly once. So, it will be O(n)

- Nitin Gupta March 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How about this test case: {6,7,6} k = 2
ans: {7,7}?

- Kevin March 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

When an element enters the queue from the back, it can knock out all the smaller elements in front of it, because those elements are also older than it. Since every new element enters the queue at its proper position in terms of bigness/smallness, you are guaranteed that the queue preserves order.

An element may eventually reach the front of the queue, even though it's too old to actually count for anything, but @nitingupta180 handles that by just dropping them on the floor when they reach the front if they're too old. I suppose you will occasionally have multiple elements at the front of the queue that need to be dropped, but this will be balanced by occasions where the front element is young enough.

- showell30@yahoo.com March 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution thanks .... nitingupta :)

- goelrinku90 March 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this solution time-complexity is O(nk). The worst case of the loop here should be K-1.

while(!Qi.empty() && arr[i] >= arr[Qi.back()])
               {
                    Qi.pop_back();
               }

- SkyClouds March 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

@SkyClouds: you are correct that the worst case of any one execution of that loop is in fact O(K). However, consider that each element can be inserted and removed at most once. This means that even though any one execution of the loop could be O(K), the total runtime of all the executions of that loop is bounded by O(N). In other words, there can be some very slow executions of that loop, but you can't hit the worst case every time.

- eugene.yarovoi April 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

def findMaxinK(a,k):
    max_ = a[0]
    for i in range(len(a)):
        if max_<a[i]:
            max_=a[i]
        if (i+1)%k==0:
            print max_
            if i!=len(a)-1:
                max_ = a[i]

- Sandip March 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why are people giving such complicated solutions when there is this simple answer.? is something wrong with this code.? i thought the same answer.please let me know if there is something wrong with this.

- akie July 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

The complexity of DashDash's solution is O(nlog(k)), Jack's complexity is O(nk). Is there solution with complexity O(n)?

- Leo March 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm wondering the same.

Your question essentially becomes: is there a data-structure where we can store k-elements, have the ability to remove one, add one, *and* find the min in O(1). I think the answer there is no: if you add or remove in O(1), the data cannot possible have any ordering/structure applied to it, so we can't get the min in O(1). If we want min in O(1), we're going to have to store it in some fashion that lets us extract that information quickly, and I think a min-heap is the best at just log(k) (as you said).

- jay March 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree with @jay, but I could just be lacking imagination. It's pretty easy to construct tricky scenarios for k=5:

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
11 12 13 14 15 80 90 100 70 60 6 7 8 9 10

My hunch is that if you design a data structure that performs well on the former, it's probably gonna churn on the latter, and vice versa.

- showell30@yahoo.com March 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class test{
  public static void main(String[] args){
//	ArrayList<Integer> a=new ArrayList<Integer>();
	  int max=Integer.MIN_VALUE, index=-1;
	int[] a={6,5,2,3,1,0};
	int k=2;
	System.out.println(a.length-k);
	for (int i=0; i<a.length-k;i++){
		if (index<i){
			max=Integer.MIN_VALUE;
			for (int j=0;j<k;j++)
				if (a[i+j]>max){
					max=a[i+j];
					index=(i+j);
				}
		}
		else
			if(a[i+k]>a[index]){
				max=a[i+k];
				index=i+k;
			}
	System.out.println(max);
	}
//  System.out.println();
  }
}

- Water March 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void Main(string[] args)
        {
            int[] parentArray = new int[] {11, 12, 13, 14, 15, 80, 90, 100, 70, 60, 6, 7, 8, 9, 10};
            int subArraySize = 0;
            Console.Write("Enter the sub array size: ");
            bool result = int.TryParse(Console.ReadLine(), out subArraySize);

            if (subArraySize <= 0 || subArraySize >= parentArray.Length)
            {
                Console.WriteLine("Size out of bounds");
                return;
            }

            for (int i = 0; i < (parentArray.Length - subArraySize + 1); i++)
            {
                int temp = parentArray[i];
                for (int j = 0; j <= subArraySize-1; j++)
                {
                    temp = Math.Max(parentArray[j+i], temp);
                }
                Console.Write(temp.ToString() + " ");
            }
        }

- Praveen March 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def findMaxinK(a,k):
    max_ = a[0]
    for i in range(len(a)):
        if max_<a[i]:
            max_=a[i]
        if (i+1)%k==0:
            print max_
            if i!=len(a)-1:
                max_ = a[i]

- shinde.sandip.s March 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int main()
{
int n,k,i,j;
scanf("%d %d",&n,&k);
int max;
int arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n-k+1;i++)
{
max=-1;
for(j=i;j<k+i;j++)
{
if(arr[j]>max)
{
max=arr[j];
printf("max %d \n",max);
}
}
printf("%d",max);
}
return 0;
}

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

#include <stdio.h>

int main()
{
int n,k,i,j;
scanf("%d %d",&n,&k);
int max;
int arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n-k+1;i++)
{
max=-1;
for(j=i;j<k+i;j++)
{
if(arr[j]>max)
{
max=arr[j];
printf("max %d \n",max);
}
}
printf("%d",max);
}
return 0;
}

- gunendu March 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

wi this could be the better soln for this????
#include<stdio.h>
struct list
{
int data;
struct list *next;
}*root=NULL;
int sorted_insertion(int n)
{
struct list *node=root,*temp;
if(root==NULL){
node=new list;
node->data=n;
node->next=NULL;
root=node;}
else
{
if(n>root->data)
{
node=new list;
node->data=n;
node->next=root;
root=node;
}
else
{
temp=root;
while((temp->next!=NULL)&&(temp->next->data>n))temp=temp->next;
if(temp->next==NULL){
node=new list;
node->data=n;
node->next=NULL;
temp->next=node;}
else
{
node=new list;
node->data=n;
node->next=temp->next;
temp->next=node;
}
}
}
return 0;
}
int display()
{
struct list *temp=root;
int count=0;
while(temp&&count<3)
{
printf("\t%d",temp->data);
temp=temp->next;
count++;
}
return 0;
}
int main()
{
int a[10],n,m,b[10],k=0,i,j,temp;
printf("enter the number of elements in the array");
scanf("%d %d",&n,&m);
printf("\nenter the array elements:\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
if(a[i]<=m)
sorted_insertion(a[i]);
}
printf("thus the nearest elements are:\n");
display();
return 0;
}

- ram rs March 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void maxOfSubArray(int mainArr[],int k) {
for(int i=0;i+(k-1)<=mainArr.length-1;i++) {
int max=-1;
for(int j=0;j<k;j++){
if(mainArr[i+j]>max)
max = mainArr[i+j];
}
System.out.println(max+" ");
}
}

Time complexity would be O(nk)
where n is size of array.

- Nagaraju Jammula March 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is an O(nk) solution. But this is obvious solution. I am looking for more efficient solution.

package array;
import java.util.Arrays;

public class MaxEachSub {

	public static int[] findMax(int[] a, int k) {
		if (k > a.length) {
			return new int[] { Integer.MIN_VALUE };
		}

		int[] output = new int[a.length - k + 1];

		for (int i = 0; i <= a.length - k; i++) {
			output[i] = max(a, i, i + k - 1);
		}

		return output;
	}

	private static int max(int[] a, int beg, int end) {
		int max = a[beg];

		for (int i = beg + 1; i <= end; i++) {
			if (a[i] > max) {
				max = a[i];
			}
		}

		return max;
	}

}

- CodeNameEagle April 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int findmax(int a[], int start, int end);

int main()
{
	int arr[] = {8,9,2,5,0,8,1,3,5};	
	int n = sizeof(arr)/sizeof(arr[0]);		
	int i, j, k, max;
	i = 0;
	
	printf ("Enter k\n");
	scanf ("%d", &k);								
	
	j = i + k;
	
	while (j <= n)
	{
		//printf ("\ni : %d\nj : %d\n", i, j);
		max = findmax(arr, i, j);						
		printf ("%d ", max);
		i++;
		j++;
	}
	return 0;
}

int findmax(int a[], int start, int end)
{
	int i;
	int max = a[start];
	for (i = start + 1; i < end; i++)
	{
		if (a[i] > max)
		{
			max = a[i];
		}
	}
	return max;
}

- rohith August 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int findmax(int a[], int start, int end);

int main()
{
	int arr[] = {8,9,2,5,0,8,1,3,5};	
	int n = sizeof(arr)/sizeof(arr[0]);		
	int i, j, k, max;
	i = 0;
	
	printf ("Enter k\n");
	scanf ("%d", &k);								
	
	j = i + k;
	
	while (j <= n)
	{
		max = findmax(arr, i, j);						
		printf ("%d ", max);
		i++;
		j++;
	}
	return 0;
}

int findmax(int a[], int start, int end)
{
	int i;
	int max = a[start];

	for (i = start + 1; i < end; i++)
	{
		if (a[i] > max)
		{
			max = a[i];
		}
	}
	return max;
}

- rohith August 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Use a max heap of size 3. Add the next element to the position where the first element is and then call heapify

- DashDash March 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

private static void FindElements(int[] a, int k)
        {
              int count = 0;
            while (count <= a.Length-k)
            {
                int max = int.MinValue;
                for (int i = count; i < count + k; i++)
                {
                    if (a[i] > max) max = a[i];
                }
                Console.Write(max + " ");
                count = count +1 ;
            }
        }

- Jack March 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why size 3 please clear your solution ....
is 3 means size of k .... ?

- goelrinku90 March 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

The complexity of DashDash's solution is O(nlog(k)), Jack's complexity is O(nk). Is there solution with complexity O(n)?

- Leo March 19, 2013 | 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