Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

This problem lends its self well to dynamic programming solution.

Note:
1.) Notice that the 3x3 solution rooted at A(i,j) is the same as the 2x2 solution with one more row and column added to the ends.
2.) Only valid solutions exist for even matrix.

javaish code:

findEqualSquare(int[][] A) {
    s = max(A.n, A.m)
    List<SquareMatrixStruct> answer
    answer.addAll(s, A)
    return answer
}

findEqualSquare(int s, int[][] A) {
    totalSize = s^2
    List<SquareMatrixStruct> answer = new SquareMatrixStruct[]
    for(i=0; i < n - s - 1; i++){
        for(j=0; j < n - s - 2; j++){
            Integer a = previousValues(i,j) // assume static HashMap 
             //where key is two int's, and value is Integer
            int b = 0;
            if(a==null){
                if(s == 1) {
                    a = A(i,j)
                }
                else {
                    findEqualSquare(s - 1,A)
                    a = previousValues(i,j)
                }
            }
            col=j + s
            row=i + s
            for(k=i; k < i + s; k++){
                b+=A(k,col)
            }
            for(p=j; p < j + s; p++){
                b+=A(row,p)
            }
            previousValue.put(i,j,s,b)
            if(s % 2 == 1 && b == totalSize / 2){
                answer.add(new SquareMatrixStruct(i,j,s))
            }
        }
    }
    return answer;
}

For notation sake we declare that n > m; if this is not true, switch notion of n and m to make it true.

Each operation should do no more additional work than 2(n-1)+1
That means that this takes O(n*((2n-1)+1)*m) work. This means that the operation works in O(n^2*m) time. Space is O(n*m) if we reuse (i,j) positions (This should be fine since we are preceding in a stepped fashion, first doing all s=1, then s= 2, ...).

I do not know of a better time algo. If you have one please share.

Disclaimer, uncompiled pseudocode

- Chris December 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Does this problem have an optimal substructure property? Will "some" of the resulting solution's sub-matrix be also an optimal submatrix with equal 0's and 1's?

- Learner December 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

divide & impera

solve(A):
if A not a solution
then
  solve(A[1..m][0..n])
  solve(A[0..m][1..n])
  solve(A[0..m-1][0..n])
  solve(A[0..m][0..n-1])
else return A

begin with solve(A[0..m][0..n])


Speedups:
1. keep two LUT with number of ones on each line and column the computation is simpler o(1) for the number of 1 and 0 on every submatrix
2. in case that the size of the matrix is even skip computation and go directly to the next

- bulah January 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

replace all the 0's by -1's. take an auxiliary matrix of the same dimension (m X n). compute it's [i, j]-th entry as B[i,j] = A[i-1, j]+A[i, j-1]-A[i-1, j-1], where A is the transformed( all 0's replaced by -1's) original matrix and B our new aux matrix. the final part is to traverse B and find tuples (i, j) such that B[i, j] = 0 and compute the maximum i*j from those tuples. this can be done in O(m X n). lastly, convert all the -1's in A to 0's.

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

correction - B[i, j] = A[i-1, j]+A[i, j-1]-A[i-1, j-1] + A[i, j]

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

This doesn't make sense to me. Can you explain the reasoning?

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

Yes, I thought of this approach but here we are only considering the sub matrices which have their Top-Left corner fixed at (0, 0)

The maximum submatrix (with equal 1's and 0's ) could be anywhere in the grid. Am I missing something ?

- Anonymous December 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 8 vote

for 1 dimension array this problem can be solved with complexity O(m), (www(dot)geeksforgeeks(dot)org/archives/20586) m being the length of each row. if we make nc2 combination of rows and sum them up and apply the same algorithm as for 1D row. Hence Complexity O(n^2*m).

- iictarpit November 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

do not quite understand what you want to do when you say make nC2 combinations of rows and sum them up? can you please elaborate a little?

- The Artist November 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm not saying you can't get an algorithm with O(n^2*m) complexity, but this solution isn't quite right. You can't just calculate the max 1D solution for each row and then try every contiguous block of rows, if I'm understanding your idea correctly. That's because the parts of the rows you select also need to line up and form a rectangle.

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

The algo in the blog is cool....

One small change, we dont have to apply it for each row... instead convert the same algorithm to 2D using "summed area table"....

Search "summed area table" and read the wiki page for more details....

1. replace 0's with -1.
2. calculate the summed area table.
3. Hash each value in the table. If it is not in the table "add the 2D index" otherwise check whether the size of the rectangular box is greater than the max_size and update the max_size if necessary.

In worst case, the algorithm is O(mn) excluding the "summed area table" calculation.

- Please correct me if I am wrong

Thank you

- prabu November 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@prabu: I don't think you've fully thought this through. The 2D case is more complicated than the 1D case.

In the 1D case, a subarray of sum 0 corresponded to a duplicate value in the sum array, and you could just use an algorithm for detecting duplicates. A duplicate in the sum array would correspond to a zero-sum subarray.

There is no such direct correspondence in the 2D case. Finding two identical values in the summed area table does not correspond to a rectangle whose sum is zero.

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

@eugene: Thank you for your feedback.... I did fail to analyze the case properly..... It is complicated ...

- prabu November 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if you call the summed array as S then you will have to find 2 points (x1,y1) and (x2,y2) such that S[x1][y1] + S[x2][y2] - S[x1][y2] - S[x2][y1] = 0 and (x1 - x2)*(y1 -y2) is maximum for 0 <= x < m, 0 <= y < n. Even in that case you will have to consider the cases when x1 = x2 or y1 = y2 seperately (this corresponds to the case when the output array is a 1 D array). If you really try to do all this, it will become a O((mn)^2) algorithm again.

- The Artist November 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is BS.

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

What if we take array from -mn to mn

- Srikant Aggarwal November 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Srikant: what do you mean?

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

@Eugene..

The answer given in geeksforgeeks can be extended to a 2D (nXm) case.
Algo :
a. Substitute 0 by -1
b. For each columnwise, nC2 pairs exist.
The sum for each pair calculation will take O(n^2) and will vary from -n to +n (if we consider paid 1, n) at max.
c. As we traverse the matrix along a row, the sum can at max vary from -m*n to +m*n. So maintaining an array from -m*n to m*n and applying the method in geeksforgeeks the solution can be arrived at O(n^2*m).

- Srikant Aggarwal December 03, 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