Cloudera Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
public class Solution {
public void setZeroes(int[][] matrix) {
HashMap<Integer,Boolean> row = new HashMap<Integer,Boolean>();
HashMap<Integer,Boolean> col = new HashMap<Integer,Boolean>();
LinkedList<int []> pts = new LinkedList<int[]>();
for (int c = 0; c < matrix.length; c++){
for (int r = 0; r < matrix[c].length;r++){
if(matrix[c][r] == 0){
pts.add(new int[]{c,r});
}
}
}
for(int [] pt : pts){
if(!row.containsKey(pt[1])){
row.put(pt[1],true);
for (int i = 0; i < matrix.length;i++){
matrix[i][pt[1]] = 0;
}
}
if(!col.containsKey(pt[0])){
col.put(pt[0],true);
for (int i = 0; i < matrix[0].length;i++){
matrix[pt[0]][i] = 0;
}
}
}
}
}
You are doing extra work for repeated rows and/or columns.
- lasthope November 20, 2013