Microsoft Interview Question for Developer Program Engineers


Country: India
Interview Type: Written Test




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

you cant reduce the time complexity further than O(n2) becoz... u have to visit each element at least once , ofcourse, so, tc > O(n2).
further dont use hashing to check for rach rows and columns as it will take space complexity of O(n).
instead,
assign each character from a to z a unique prime number.
find the multiplication of these 26 prime numbers, say m
now find the multiplication of 26 characters present in each rows and columns
if each rows and each columns multiplication match to m.
so return true
else return false..

furthermore, dont think abt to reduce time complexity further.. u cant
u have to go through each element at least for once

- guest travis August 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry tc >= O(n2)

- guest travis August 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you map a-z to 26 unique primes, it will have a space complexity of O(n), since you need to store it ..
it will take the same space complexity as hashing. BTW hashing doesnt need to do multiplication and the look up..

- Evan August 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

dont store it .. use if else statement..as you know which prime numbers u have assigned to which character
so, to reduce space complexity
if( ch =='a') ...
if(ch =='b') ....
...
and so on

- guest travis August 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

evan... i guess it is clear to u..

- guest travis August 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@travis thanks... make sense now

- Evan August 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What you do not understand however, is the fact that right now we are given a matrix with a definite number of elements (Order(n) does not apply since n isn't variable). There are 676 of them to be precise. When you talk about finding the multiplication of each row and column, you are visiting each element twice. Therefore, number of calculations performed are equal to 676*2. Instead, if you take two arrays of size 26 each and store the cumulative result, you will have to visit every element only once. [See the code I posted below], thus reducing the number of calculations to 676.

I guess this problem is aimed to emphasize that sometimes the order doesn't matter, a heavy computation that takes seconds to complete should be used as less as possible. Obviously, a code with lesser computations would run considerably faster, even if its time complexity is equal to some other code (with more computations).

- nj August 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@nj What if you add numerical letters to it? What if another language? Order matters.

- dc360 August 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Assigning prime number and multiplying seems a good method.But if you notice ,the situation is similar to the problem below.

problem:Given n consecutive numbers in an array,find whether one of the numbers is replaced with a duplicate of another number.
eq: 1,2,3,4,5 {4 is replaced with another 3} which makes it 1,2,3,3,5 .We need to find if such a replacement occurred or not.Find the sum of all numbers.subtract this from what the actual sum wouldve been had they been consecutive n*(n+1)/2
where n = 5 so real sum should have been 15.But actual sum is
14.Therefore duplicate exists.

Now replace the value of char with an integer value giving "a" value 1.We can get the missing character.Of course the complexity remains same.

- sushanth September 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good luck with finding 26 primes in an interview!!!
Hashing does not take O(N), we just need to keep track of 26 values, a single integer would do if you use bitmap.

- IntwPrep December 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

take a[52][26] as extra space
now take first element in b[26][26] (given array) say index i=0,j=0 (consider the value inside this array as 0-25 instead of a-z) for example if b[0][0]='c';(here c considered as 'c'-'a'-1=2)
now assign a[0][2]=1 and a[26][2]=1 do like this for entire given array while filling in array a[][] if already it is 1 then the value is repeating if not the given array is perfectly valid.
give your suggestions

- sukusuku August 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think I can push your algor a little further.

you can use 52 integers instead of a 2-D array.. since one int can store 32 bits( in this case, you only need 26 bits)

- Evan August 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Evan yes you can .hope fully it can be done in o(n)

- sukusuku August 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void check()
{
String scol = "" ; String srow="";
for(int i = 0 ; i < 26 ;i++)
{
scol = " ";
srow= " ";
for(int j = 0; j<26 ; j++)
{
scol = scol + a[i][j];
srow = srow + a[j][i];
// call method to compare scol and srow for presence of all alphabets . Continue till end of row

}

O(n2)

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

Here I am using Set variable to check . For every row data will be inserted to this set variable , if after traversing a row size of set is not 26 then return false ;// condition is not satisfied , otherwise go for checking next row .

public boolean checkArray(char array[][])
	{
		int rowLimit=array.length;
		int colLimit=array[0].length;
		int i,j;
		HashSet<Character> set=new HashSet<Character>();
		
		for(i=0;i<rowLimit;i++)
		{
			for(j=0;j<colLimit;j++)
			{
				set.add(array[i][j]);
			}// End of inner for loop
			
			if(set.size()!=26)
			{
				return false; // each row is not having char from a-z
			}
			else
			{
				set.clear();
			}
		}// End of outer for loop
		return true; // each row satisfy required condition
	}

- Gaurav August 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

all codes having time complexity O(n^2) anyone with efficient approach
...

- abhishek August 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

public static final int OR= 0x3ffffff;

    public static boolean isValid(char[][] array, int pos, int[] orRow, int[] orCol)
    {
        if(pos==array.length-1)
            return (orCol[pos]|array[pos][pos])==OR&&(orRow[pos]|array[pos][pos])==OR;
        
        int cumRow= orCol[pos], cumCol= orRow[pos];
        
        for(int i=pos; i<array.length; i++)
        {
            cumRow|=0x1<<((int)((int)array[pos][i]-(int)'a'));
            cumCol|=0x1<<((int)((int)array[i][pos]-(int)'a'));
            
            orRow[i]|=0x1<<((int)((int)array[pos][i]-(int)'a'));
            orCol[i]|=0x1<<((int)((int)array[i][pos]-(int)'a'));
        }
        
        return (cumRow==OR&&cumCol==OR)?isValid(array, pos+1, orRow, orCol):false;
    }

- nj August 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think, it is good solution, but recursion is not needed here.
Simple nested loops are good enough.

- Alex August 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is solution. It is O(n*n) and additional 8 bytes for storing bits. (does not count local variables). Written on C#. It is possible to eliminate "prev" variable by using "|=" operator, but it will be hard to read.

static int getCharIndex(char c)
    {
      return c - 'a';
    }
    static bool checkArray(char[,] a)
    {
      const int maxChars = 26;
      if (a == null || a.Rank != 2)
        return false;
      int N = a.GetUpperBound(0) + 1;
      if (N > maxChars || a.GetUpperBound(1) + 1 != N)
        return false;
      for (var i = 0; i < N; i++)
      {
        uint row = 0;
        uint col = 0;
        for (var j = 0; j < N; j++)
        {
          int iRow = getCharIndex(a[i, j]);
          if (iRow < 0 || iRow >= N)
            return false;
          uint prev = row;
          row = row | (1u << iRow);
          if (prev >= row)
            return false;
          int iCol = getCharIndex(a[j, i]);
          if (iCol < 0 || iCol >= N)
            return false;
          prev = col;
          col = col | (1u << iCol);
          if (prev >= col)
            return false;
        }
      }
      return true;
    }

- Alexander August 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think by using bit mapping it can be solved in o(n*n)
we can have one number.
int l=0;
then
for all i and j
int bit=(a[i][j]/97)*(a[i][j]%97)
if(l&pow(2,bit)!=0)
return false;
l=l | pow(2,bit)
end for

- rahul August 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hey!! I am missing something here, can't we just sum up all the elements for each row and each column to check if all sums are the same unique value. ?

- Mike August 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1 + 2 + 3 + 4 + 5 = 15
also
1 + 3 + 3 + 3 + 5 = 15

Just because the sum is 15, you can't say that the numbers are unique.

- dc360 August 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think if we want to have different alphabet in rows and column the string should be rotated while entering in each row..
abc
bca
cab

- piyush September 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think if we want to have different alphabet in rows and column the string should be rotated while entering in each row..
abc
bca
cab

- piyush September 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Each element is accessed once


#include <stdio.h>
void main()
{
char arr[26][26];
int i,j,k=0;
for(i=0;i<26;i++)
{
k = i;
for(j=0;j<26;j++)
{
arr[j][k] = i + 97;
k--;
if(k==-1)
k=25;
}

}
for(i=0;i<26;i++){
for(j=0;j<26;j++)
{
printf("%c ",arr[i][j]);
}
printf("\n");
}
}


Complexity: O(n^2)

- Anonymous August 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I believe what you are doing is to create a matrix that each row and column contain a to z exactly once instead of checking a matrix

- Evan August 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

const int N = 26;

static inline unsigned long get_character_mark(const char a)
{
	return (1UL << a - 'a');
}

bool judge_matrix(char arr[N][N])
{
	unsigned long sum_arr[2][N];
	memset(sum_arr, 0, sizeof(sum_arr));

	for(int i = 0; i < N; ++i)
	{
		for(int j = 0; j < N; ++j)
		{
			const unsigned long mask = get_character_mark(sum_arr[i][j]);
			if((sum_arr[0][i] & mask) || (sum_arr[1][j] & mask))
			{
				return false;
			}
			else
			{
				sum_arr[0][i] |= mask;
				sum_arr[1][j] |= mask;
			}
		} // for
	} // for

	return true;
} // judge_matrix

- Anonymous August 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 8 vote

My approach:

Compute an XOR of a to z (a^b^c...z).
XOR this value with each row and column. If the row/column contains the set(a-z), this XOR will return 0.

The time complexity in this case will still be O(n*n), but we have at least removed the space complexity.

- chandershivdasani August 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

is this approach correct? please reply guys :)

- riteshkyal August 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, it's not.
If characters a to z are 8-bit ascii values we have at most 2^8 (=256) possible values for xor result. On the other hand for each row/column there are choose(51,26) possible ordered combinations with repetition, which is far greater number.

Therefore, there are at least two different combinations which give the same xor result.

- dev.cpu August 25, 2012 | 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