NetApp Interview Question for Software Engineer / Developers






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

Since the matrix is sorted so is the submatrices.
1. compare key with A[m/2][n/2]
2. if key > A[m/2][n/2] , then search in the lower right submatrix
3. if key < A[m/2][n/2] , then search in the upper left submatrix
4. else if (key == A[m/2][n/2]) ret TRUE
5. else ret FALSE

recursively follow this

This gives a O(log(MN)) solution which is better than O(m)

Correct me if i am wrong

- jayeshr007 December 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It says the rows and the columns are sorted. Your logic would not detect 50 in this 3X3 matrix
1 2 3
2 3 4
50 60 70

- Anonymous January 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Matrix is A[M][N], each row and column is sorted in ascending order.
2. For i=0 to M-1
---->if (i < N)
------> if (A[i][i] < Key)i++;
3. i--;
4. Do binary search in A[0][i] and A[i][0]
5. Do binary search in A[0][i+1] and A[i+1][0]
Time Complexity O(M)+O(log(M))+O(log(N)) == O(M)

- Tulley January 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

google saddleback search

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

<pre lang="java" line="1" title="CodeMonkey63692" class="run-this">public class Saddleback {
public static String saddleback_search(int matrix[][], int elem)
{
int h = 0;
int k = matrix[0].length-1;
int rowCount = matrix.length-1;
while (matrix[h][k]!=elem && h<rowCount && k>0)
{
if (matrix[h][k]<elem)
h++;
if (matrix[h][k]>elem)
k--;
if (matrix[h][k]== elem)
return Integer.toString(h)+","+Integer.toString(k);
}
return "NOT FOUND";

}
public static void main(String args[])
{
int matrix[][]=new int[4][4];
matrix[0][0]=2;
matrix[0][1]=2;
matrix[0][2]=3;
matrix[0][3]=5;
matrix[1][0]=3;
matrix[1][1]=4;
matrix[1][2]=5;
matrix[1][3]=6;
matrix[2][0]=3;
matrix[2][1]=5;
matrix[2][2]=6;
matrix[2][3]=8;
matrix[3][0]=3;
matrix[3][1]=6;
matrix[3][2]=7;
matrix[3][3]=9;
System.out.println(Saddleback.saddleback_search(matrix, 9));
}
}

</pre><pre title="CodeMonkey63692" input="yes">This Java code should work.</pre>

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

If sorting is done in increasing order: start with the top rightmost element and if current-value < element to search, move down, and if current-value> element to search move left. Continue this till one of the index values become negative(that is you have moved out of the array).

Similar approach will be used for decreasingly sorted array.

- ramblersm July 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

lets the array

   1  2  3  4  5
   6  7  8  9  10
   11 12 13 14 15
   16 17 18 19 20
   21 22 23 24 25

start from top right element i.e.. 5, if the target number is greater than a[5,5] then search with a[1,5] else a[0,4] continue this one till find the number

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

<------|
   1  2  3  4  5  |
   6  7  8  9  10 |
   11 12 13 14 15 |
   16 17 18 19 20 |
   21 22 23 24 25

- Anonymous January 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

lets the array

   1  2  3  4  5
   6  7  8  9  10
   11 12 13 14 15
   16 17 18 19 20
   21 22 23 24 25

start from top right element i.e.. 5, if the target number is greater than a[5,5] then search with a[1,5] else a[0,4] continue this one till find the number

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

The complexity here is O(m) + O(n). This can be achieved with complexity O(Log(mn)) by applying a binary search on the columns and then a binary search on the selected row..

- Kailash. March 07, 2011 | 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