Skip to content

Instantly share code, notes, and snippets.

@pyrofolium
Last active October 11, 2017 02:22
Show Gist options
  • Select an option

  • Save pyrofolium/a08b8d896954c57a21c0820cd384f57b to your computer and use it in GitHub Desktop.

Select an option

Save pyrofolium/a08b8d896954c57a21c0820cd384f57b to your computer and use it in GitHub Desktop.
questions used in the last interview.
function recursive_mult(x, y) {
if(y === 0 || x === 0) {
return 0;
}
return x + recursive_mult(x, y-1);
}
console.log(recursive_mult(3,2));
//[[int]] -> int
function number_of_islands(matrix){
var islands = 0;
for(var i = 0; i< matrix.length; i++){
for(var j = 0; j< matrix[0].length; j++){
var value = matrix[i][j];
if(value === 0){
continue;
} else {
flood_fill(matrix, i,j);
islands += 1;
}
}
}
return islands;
}
function flood_fill(matrix, i, j){
if(matrix[i][j] === 0 || i<0 || j<0 || i>=matrix.length || j>=matrix[0].length){
return;
}
matrix[i][j] = 0;
flood_fill(matrix, i,j-1);
flood_fill(matrix, i, j+1);
flood_fill(matrix, i+1,j);
flood_fill(matrix, i-1,j);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment