Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

let the array be of size mxn,

int i=0, j=n-1;
while(i<m && j>=0){
  if(arr[i][j]==num)
    print("number found at:", i + ":" + j);
  if(arr[i][j]>num)
    j--;
  if(arr[i][j]<num)
    i++;
}
}

- arham December 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Your algo works in O(n) but binary search algorithm can be applied in 2-D array also and the problem can be solven hi O(log n) and this was the answer expected by the interviewer.

you need to perform binary search in rows and columns depending upon your value..

- Pankaj February 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

binarySearch on 2D matrix:


#include <stdio.h>
#include <stdlib.h>

int rows = 50;
int cols = 50;

int** matrix = NULL;

int search_matrix(int start_row, int start_col, int end_row, int end_col,
int search_term) {

int returnValue = -1;

int mid_row = (start_row + end_row) / 2;
int mid_col = (start_col + end_col) / 2;

if (start_row > end_row || start_col > end_col || end_row < start_row
|| end_col < start_col || search_term < matrix[0][0])
return -1;

if (matrix[mid_row][mid_col] == search_term) {
printf("\nFound %d at Row = %d Column = %d\n", search_term, mid_row,
mid_col);
return mid_row;
}

if (matrix[mid_row][start_col] <= search_term
&& matrix[mid_row][cols] >= search_term) {
// we have found our row

if (matrix[mid_row][start_col] <= search_term
&& matrix[mid_row][mid_col] >= search_term) {
start_row = mid_row;
//start_col = 0;
end_row = mid_row;
end_col = mid_col;
}

else if (matrix[mid_row][mid_col] < search_term
&& matrix[mid_row][cols] >= search_term) {
start_row = mid_row;
start_col = mid_col;
end_row = mid_row;
end_col = cols;
}

returnValue = search_matrix(start_row, start_col, end_row, end_col,
search_term);

} else if (matrix[mid_row][0] > search_term) {
end_row = mid_row;
end_col = cols;

start_row = 0;
//start_col = 0;

returnValue = search_matrix(start_row, start_col, end_row, end_col,
search_term);

} else if (matrix[mid_row][0] < search_term) {

start_row = mid_row + 1;
//start_col = 0;

end_row = rows;
end_col = cols;

returnValue = search_matrix(start_row, start_col, end_row, end_col,
search_term);
}

return returnValue;
}

int main(int argc, char** argv) {

int i = 0, j = 0, counter = 0;

matrix = (int**) malloc(sizeof(int) * rows);
for (i = 0; i < rows; i++) {
matrix[i] = (int*) malloc(sizeof(int) * cols);
}

// initialize matrix
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
matrix[i][j] = counter + j;

counter = matrix[i][cols - 1] + 1;
}

for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
printf("%d ", matrix[i][j]);

printf("\n");
}

cols = cols - 1;
rows = rows - 1;
int searchTerm = 2423;

int index = search_matrix(0, 0, rows, cols, searchTerm);

if (index == -1)
printf("\n%d not present in matrix\n", searchTerm);

return EXIT_SUCCESS;
}

- Jester December 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@manikantansubramanyam: what does this code do??? above code works fine, its just 5 lines of code!!!

- Anonymous December 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

since the matrix contains sorted numbers, its more efficient to not look at every possible index of the matrix. Looking at every index will make the search O(n^2). but doing a binary search would eliminate rows and cols where our search term is not expected to be. Hence, the efficiency of this would be O(log n).. thats the advantage.

- Jester December 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This wouldn't work for an input like
{10, 20, 30, 40},
{15, 25, 35, 45},
{26, 31, 38, 48},
{32, 34, 39, 50} where the value at a particular i,j can be greater than the range for the previous row and vice versa.
I'm not able to come up with something better than O(n) [ check for the last value in the row approach and then do a binary search on the row which would result in O(n) + O(log(n))]. Can someone elaborate on a better method.

- sujith January 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the above code would work on inputs where the matrix sequence is strictly increasing across rows, columns and adjacent rows.

@sujith for your input, i'm not sure how to find a row and do a binary search in [O(n) + O(log(n))] time. lets say we're searching for the number 38, now as per your logic, you'll first loop over all the rows comparing the value of matrix[i][0] and matrix[i][n-1] with 38. For each such satisfying row, you will need to do a binary search to get the actual column of 38 (if it exists in that row). This will take you O(n*log(n)) time in the worst case. For example, if we search for 38, we'll have to do a binary search on Row 1(assuming the row count starts at Row 0), and Row 2 before we get the desired data.

- Jester January 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks @jester. realized it wouldn't work either..
I guess the best solution here is to eliminate 1/4th of the matrix depending on whether the element is greater than the mid element and recursively keep finding the element in the remaining 3 sub matrices..
A very neat solution would be to start at the bottom left(or top right position) and have counters to step up and step right. This would be O(m-1 * n-1) which is not as good as the recursive approach

- sujith January 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The above code wouldn't work because - for example - if we find a row where the first element < target and last element > target, we haven't necessarily found the row containing the element - it could be in another row altogether!

- eugene.yarovoi January 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what the fk is that

- om471987 August 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@manikantansubramanyam: the above code is o(m+n). Actually, it will take m+n steps in the worst case.

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

@Annonymous very true dude.. the above algorithm is linear in O(m + n). but my point was that we can do better by using binary search which has O(log n)..

- Jester December 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This question is explained in detail in Cracking the Code Interview

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

Hey....

Use Binary Search Algorithm modified for 2D Array. It will work at O(log n), for an array of mxn or nxm, where n>m.

- Gopi December 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I know of no general algorithm that can do a binary search in two dimensions. I could be wrong, but I think I remember my algorithms professor not knowing of such an algorithm either.

- eugene.yarovoi January 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pseudo code according to me:
tuple (x,y) = (0,0).
tuple (m,n) = (m,n)
then check if a[0][m/2] > num and [n/2][0] >num
decide accordingly and change the tuples correspondingly .
recursively search.

basically my logic is to half the matrix in each recursive call.

- jpdasit17 January 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This would work if you were guaranteed that at least one of the two comparisons you mention returned true. However, there's no reason why they both couldn't return false.

- eugene.yarovoi January 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

here is a good solution.

puddleofriddles.blogspot.com/2011/07/given-matrix-in-which-each-row-and-each.html

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

This is log(n) algorithm:

First we want to locate the row(may be one or two such row exists) at which our target number locate by applying Binary Search for the last column.(log(n))

Second, apply Binary Search for that row to locate our target number.(log(n))

- dv April 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A O(log(n)) solution:

int binarySearch(int n, vector<int> &A, int s, int e) {
	if(s >= e) return s;
	
	auto m = (s + e) / 2;
	auto v = A[m];
	if(v == n) return m;
	
	if(v < n) return binarySearch(n, A, m + 1, e);
	return binarySearch(n, A, s, m - 1);
}
struct Point {int i; int j; Point(int i_, int j_){i=i_;j=j_;}};
Point find(int n, vector<vector<int>> &A) {
	auto size = A.size();
	int row = 0, col = 0, prevCol = 0, prevRow = 0;
	while(A[row][col] < n) {
		if(A[row][size-1] >= n) {
			col = binarySearch(n, A[row], col, size - 1); //find the cell just less than the search value in the current row
			if(A[row][col] > n) { //binary search returned the higher cell, so decrement by 1
				col--;
			}
		}
		if(A[size - 1][col] >= n) {
			vector<int> column;
			for(auto r = row; r < size; r++) {
				column.push_back(A[r][col]);
			}
			row = binarySearch(n, column, 0, size - 1 - row) + row;//find the cell just less than the search value in the current column
			if(A[row][col] > n) {//binary search returned the higher cell, so decrement by 1
				row--;
			}
		}
		if(col == prevCol && row == prevRow) {//no movement -> not found
			break;
		}
		prevRow = row; prevCol = col;
	}  
	if(A[row][col] != n) return Point(-1, -1);
	return Point(row, col);
}

- Mhret November 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The comments made it less readable. Here a version with the comments

int binarySearch(int n, vector<int> &A, int s, int e) {
	if(s >= e) return s;
	
	auto m = (s + e) / 2;
	auto v = A[m];
	if(v == n) return m;
	
	if(v < n) return binarySearch(n, A, m + 1, e);
	return binarySearch(n, A, s, m - 1);
}
struct Point {int i; int j; Point(int i_, int j_){i=i_;j=j_;}};
Point find(int n, vector<vector<int>> &A) {
	auto size = A.size();
	int row = 0, col = 0, prevCol = 0, prevRow = 0;
	while(A[row][col] < n) {
		if(A[row][size-1] >= n) {
			col = binarySearch(n, A[row], col, size - 1); 
			if(A[row][col] > n) { 
				col--;
			}
		}
		if(A[size - 1][col] >= n) {
			vector<int> column;
			for(auto r = row; r < size; r++) {
				column.push_back(A[r][col]);
			}
			row = binarySearch(n, column, 0, size - 1 - row) + row;
			if(A[row][col] > n) {
				row--;
			}
		}
		if(col == prevCol && row == prevRow) {
			break;
		}
		prevRow = row; prevCol = col;
	}  
	if(A[row][col] != n) return Point(-1, -1);
	return Point(row, col);
}

- Mhret November 16, 2014 | 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