Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

we can use kandane's to do so
getMaximumSubMatrixSum is function which needs to be called

public class MaximumSubMatrix {

	int kandane(int [] data) {
		int max =0;
		int sum =0;
		for(int i=0;i<data.length;i++) {
			sum+=data[i];
			sum = Math.max(0,sum);
			max=Math.max(max, sum);
		}
		return max;
	}
	public int getMaximumSubMatrixSum(int [][] data){
		int max =0;
		int COLS = data[0].length;
		int ROWS = data.length;
		for(int left = 0; left < COLS; left++) { // fix a left column
			int [] _data = new int[ROWS];
			for(int right = left; right < COLS; right++) { // fix a right column
				// now we will look all the data between left and right column
				for(int i=0;i<ROWS;i++)
					_data[i] += data[i][right];
				// find maximum sum in _data array using simple kandane's algo
				max = Math.max(max, kandane(_data));
			}
		}
		return max;	
	}

- javaD November 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

You have a problem with negative sums, but this would be the O(n^3) solution as stated by Jason.

- nothing special here November 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

if M is of size nxn (does not have to be a square), this is a question solved in careercup book, brute force is O(n^6), optimized with O(n^4) additional space complexity O(n^2)
Additional space complexity is for precomputing calculating sum till (i,j)
sum[i][j] = sum[i - 1][j] +sum[i][j-1]-sum[i-1][j-1]+M[I,j]
and then selecting different matrices is O(n^4), the sum calculation is O(1)
sum[i2][j2] - sum[i2][j1 - 1] - sum[i1 - 1][j2] + sum[i1 - 1][j1 - 1];
just return max sum

- notanexpert November 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is an extension of max subarrary problem, can be done in O(n^3) for a n*n matrix

- Jason November 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

so cubic is best known?

Any proof of problem's lower bounds beyond more than N^2?

- S O U N D W A V E November 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Brainless Urik , sorry, man, this is the best algorithm I have ever known and I cannot prove that this is the optimal one.

- Jason November 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Cool so its an open problem

- urik on bb November 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be easily solved with dynamic programming. Same logic as in max subarray problem.

- bulutmf April 04, 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