Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

what is rank k number of an array...

- Anonymous July 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class KRankService
{
	public static void kRank(int[] arr, int k)throws NullPointerException, IllegalArgumentException
	{
		if(arr==null)
		{
			throw new NullPointerException(“input array cannot be null”);
		}
		if(k<0||k>arr.length)
		{
			throw new IllegalArgumentException(“k cannot be less than zero or greater than array length”);
		}
		
		MinHeap mh=new MinHeap(k);
		HashMap<Integer,Boolean> inHeap=new HashMap<Integer,Boolean>(k);
		for(int i=0; i< arr.length;i++)
		{
			if(!inHeap.contains(arr[i]))
			{
				if(mh.isFull())
				{
					if(arr[i]>mh.getMin())
					{
						mh.extractMin();
					}
				}
				mh.insert(arr[i]);
				inHeap.put(arr[i],true);
				
			}
				
		}
			
		
		System.out.println(“kth rank: “ + mh.getMin());
		System.out.println(“k highest values: “ );
		while(!mh.isEmpty())
		{
			System.out.print(mh.extractMin() + “ “);
		}
	
	}
	private static int[] getInputArray(int n)
	{
	 	if(n==0)
		{
			return null;
		}
		int[] arr=new int[n];
		Random rnd=new Random();
		for(int i=0; i<arr.length;i++)
		{
			arr[i]=rnd.nextInt(n);
		}
		return arr;
	}
	public static void main(String[] args)
	{
		Random rnd=new Random();
		int n=rnd.nextInt(1001);
		int k=rnd.nextInt(51);
		int[] input=KRankService.getInputArray(n);
		KRankService.kRank(input,k);
	}
}

- divm01986 July 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Mistake in error handling code. It should be k<=0

- divm01986 July 14, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We could also use randomised quick sort n partition about kth element.

- Mayuri July 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wouldn't you need to know the kth-highest(or lowest) element to do that? Also wouldn't that be NlogN?

- Josh July 29, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <map>

using namespace std;

void arr_rank(int a[], int n)
{
  map <int, int> count;
  map <int, int> rank;
  
  int rankCount = 1;
  
  // construct map
  for(int i=0;i<10;i++)
  {
    count[i] = 0;
    rank[i] = 0;
  }
  
  for(int i=0;i<n;i++)
  {
    count[a[i]] += 1;
  } 
  
  for (int i=0;i<10;i++)
  {
    if (count[i] > 0)
    {
      rank[i] = rankCount;    
      rankCount++;
    }
  }
  
  for (int i=0;i<n;i++)
    cout << rank[a[i]] << endl;

}

int main(void)
{
  int a[] = {3, 1, 3, 4, 9, 8, 5};
 
  arr_rank(a, 7);
  
}

- Shalabh November 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

why can't we use bubble sort k times : Time complexity : O(nk)
Extra Space : O(1)

- Lakshmi Ravi November 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since sorting using bubble sort will be in O(nK) while using min Heap will be NLogK
so prefer MinHeap to BubbleSort

- akkhil2012 April 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use MinHeap and before inserting into MinHeap , we have to check whether this number exists as we can not use the hash due to the constrain of time complexity

Space => O(k) which can refer as O(1)
Time => O(nk) which can refer as O(n)

- Neelavan April 07, 2017 | 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