Adobe Interview Question for Software Engineer / Developers


Country: United States




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

1) Search in four directions from position (i,j) :
right, right-diagonal downwards, down, left-diagonal downwards.
2) Keep track of longest trail of 1's found so far. Also change 1 to 0 as you traverse a cell in matrix so that you dont need to check for that position again.
=> O(n^3)

OR

You can save at position (i,j) four values a,b,c,d:
Longest trail of 1's ending at (i,j) from four directions i.e left, right-diagnoal up, up and left-diagonal up.

temp[i][j].a = mat[i][j] == 1 ? mat[i][j-1]+1 : 0;
temp[i][j].b = mat[i][j] == 1 ? mat[i-1][j-1] + 1 : 0;
temp[i][j].c = mat[i][j] == 1 ? mat[i][j-1] + 1 : 0;
temp[i][j].d = mat[i][j] == 1 ? mat[i-1][j+1] + 1 : 0;

Answer is max( temp[i][j].a, temp[i][j].b, temp[i][j].c, temp[i][j].d ) for all cells (i,j).
=> O(n^2)

- Cerberuz October 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well n^2 is the worst case(brute force).
Simply scan
- initially for each row
- then for columns
- then for diagonals in forward and back ward direction
yes, while you do the scans each step will let to optimize for the next one. But its still n^2, being brute force.

Any better ideas ?

- torchbearer October 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Whatever algorithm you want to design, i think its almost impossible to do it without traversing the whole matrix which itself is O(n^2). Your brute force will do it in 3 passes while mine will do in single pass only :)

- Cerberuz October 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's not almost impossible, it's like, completely impossible.

- Anonymous October 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

brute force is space efficient but not yours....

- saurav.singhiitd October 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Extra space used in my approach can be avoided by storing via bit compression within the given matrix.

- Cerberuz October 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Simply do the dfs as 8 connected component.

- Psycho October 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

just a question, when you change a cell to 0 (let's say during diagonal process), you would still want to check for longest queue of 1s in that row. but if you had changed that cell to 0 in diagonal run, that won't give you correct solution for row or column when their turn come to check, would it?

- Bruce October 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

arr = [] n size array, each element is (int, int) ==> col_count, diag_count
initialize arr by the first row of matrix.
row_max = 0
col_max = 0
diag_max = 0
for i in range(1, M, 1):
  for j in range (0, N,1):
    scan for consecutive 1s, is it greater than row_max, yes change row_max
    arr[j].col_count = (row[i][j]==1)?arr[j].col_count+1:0
    arr[j].diag_count = (row[i][j]==1)?arr[j-1].diag_count+1:0
......
  and the usual routine
  if arr[j].col_count > col_max --> set col_max
......

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

Is the question also asking to consider the case wherein we might have to combined effective maximum of horizontal, vertical and diagonal trails?
Or is it just like have a result of each of these 3 and finding the maximum out of them?

- Learner October 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nopes, JUST the max trail possible considering horizontal, vertical and diagonals.
mine n^2 answer as explained above didn't quite satisfy him.
Not sure what was his expectation.

- torchbearer October 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

mat[i][j] = mat[i][j]==max(mat[i-1][j] , mat[i][j-1], mat[i-1][j-1])+1 or 0 depending on the value of mat[i][j] .

mat[n][n] gives the answer. If we modify the original matrix itself then , we don't need to use any extra space. thus , it can be done with O(n^2) and O(1) space .

Simply traverse over to find the maximum in the matrix then .

- sankalp October 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Won't work ( trail -> straight line ).

- Cerberuz October 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

maximal connected component in a graph

- akshay December 28, 2012 | 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