## Interview Question

Software Engineer / Developers**Country:**United States

**Interview Type:**Phone Interview

```
public class Attempt1{
boolean[] row = new boolean[n];
boolean[] column = new boolean[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == "x") {
row[i] = true;
column[j] = true;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (row[i] || column[j]) {
matrix[i][j] = "x";
}
}
}
System.out.println("Resultant matrix: ");
printMatrix(matrix, n);
return matrix;
}
```

Here is a solution that I can think of. Please provide inputs if there is a better way to solve it. Thank you!

```
public class StackOver1{
boolean[] row = new boolean[n];
boolean[] column = new boolean[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == "x") {
row[i] = true;
column[j] = true;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (row[i] || column[j]) {
matrix[i][j] = "x";
}
}
}
System.out.println("Resultant matrix: ");
printMatrix(matrix, n);
return matrix;
}
```

Here are my questions: 1. Is there a better way to solve this?

2. Should I consider a better data structure?

```
public class Attempt1{
boolean[] row = new boolean[n];
boolean[] column = new boolean[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == "x") {
row[i] = true;
column[j] = true;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (row[i] || column[j]) {
matrix[i][j] = "x";
}
}
}
System.out.println("Resultant matrix: ");
printMatrix(matrix, n);
return matrix;
}
```

Here are my questions: 1. Is there a better way to solve this? 2. Should I consider a better data structure?

Thank you.

Coupla quick solutions:

n squared:

Linear:

- moose October 01, 2015