## Amazon Interview Question for SDE-2s

Country: United States

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

O(m x n) solution

``````public int solve(int matrix[][]) {
int minSum = 0;
boolean isAllColumnsCovered = false;
int firstMaxColumnID = 0;
int minDiff = Integer.MAX_VALUE;

int N = matrix.length;
if (N < 1) { return 0; }
int M = matrix[0].length;
if (M < 1) { return 0; }

// 1st traversal
for (int i = 0; i < N; i++) {
int max = 0;
int max2 = 0;
int rowSum = 0;
int maxColumnID = 0;

for (int j = 0; j < M; j++)
{
if (matrix[i][j] > max)
{
if (i == 0)
{
firstMaxColumnID = j;
}
else
{
maxColumnID = j;
}

if (j == 0)
{
max = matrix[i][j];
}
else
{
max2 = max;
max = matrix[i][j];

}
}
else if (matrix[i][j] > max2)
{
max2 = matrix[i][j];
}

rowSum += matrix[i][j];
}
rowSum -= max;
minSum += rowSum;

if (!isAllColumnsCovered) {
if (i > 0 && firstMaxColumnID != maxColumnID) {
isAllColumnsCovered = true;
} else {
// Keep the min diff
if (minDiff > (max - max2)) {
minDiff = max - max2;
}
}
}
}

return minSum + (isAllColumnsCovered ? 0 : minDiff);
}``````

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

private String getStringAfterRemovingoverlappingString(String str) {
String string=null;
for(int i=0;i< str.length() && i+1 <str.length();i++){
if(str.charAt(i) == str.charAt(i+1)){
string = str.subSequence(i+1,str.length()).toString();
}
}
return string;
}

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

``````private String getStringAfterRemovingoverlappingString(String str) {
String string=null;
for(int i=0;i< str.length() && i+1 <str.length();i++){
if(str.charAt(i) == str.charAt(i+1)){
string = str.subSequence(i+1,str.length()).toString();
}
}
return string;
}``````

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

``````private String getStringAfterRemovingoverlappingString(String str) {
String string=null;
for(int i=0;i< str.length() && i+1 <str.length();i++){
if(str.charAt(i) == str.charAt(i+1)){
string = str.subSequence(i+1,str.length()).toString();
}
}
return string;
}``````

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

``private String getStringAfterRemovingoverlappingString(String str) {String string=null; for(int i=0;i< str.length() && i+1 <str.length();i++){if(str.charAt(i) == str.charAt(i+1)){        string = str.subSequence(i+1,str.length()).toString();}}return string;``

}

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.

### 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.