Last active
October 11, 2017 02:22
-
-
Save pyrofolium/a08b8d896954c57a21c0820cd384f57b to your computer and use it in GitHub Desktop.
questions used in the last interview.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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