Amazon Online Assessment | Europe | 2020

I was reached out by one of the Amazon HR. He directly send OA link and asked me to complete the same. Below are the questions.

Question 1:

https://leetcode.com/problems/number-of-islands/

Questions 2:
Similar to : https://leetcode.com/problems/rotting-oranges/

The input to the function/method consists of three arguments:
1) Rows, an integer respresenting the number of rows in the grid
2) Columns, an integer respresenting the number of columns in the grid
3) grid, an integer array representing the current status of its servers. 
The value 0 represents a server that has yet to received the file and 1 represents a server that already has the file.

Outouts:
Return an integer representing the minimun number of hours required to send the file to all the available servers. 
Return -1 if all the available servers cannot receive the file.

Note:
Diagonally placed cells are not considered adjacent./

Example:
Input:
rows = 4
columns = 5
grid = 
[[0, 1, 1, 0, 1],
 [0, 1, 0, 1, 0],
 [0, 0, 0, 0, 1],
 [0, 1, 0, 0, 0]]
 
 Output:
 2
 
 Explanationn:
 At the end of the first hour, the status of the servers:
 1, 1, 1, 1, 1
 1, 1, 1, 1, 1
 0, 1, 0, 1, 1
 1, 1, 1, 0, 1
  At the end of the second hour, the status of the servers:
  1, 1, 1, 1, 1
  1, 1, 1, 1, 1
  1, 1, 1, 1, 1
  1, 1, 1, 1, 1
  By the end of two hours, all the servers are upto date.
Code Snippet:

public class Solution {
    int miniumHours(int rows, int columns, List<List<Integer>> grid) {
  			# WRITE YOUR CODE
    }
}

OUTPUT/CODE for Question 2:

public class Solution {

     public static int miniumHours(int rows, int columns, List<List<Integer>> grid) {
				int len = grid.length;
				int breadth = grid.get(0).length;
				int max = 0;
				
				List<Cell> badCells = new ArrayList<>();
				for(int i=0; i<len; i++) {
				      for(int j=0; j<breadth; j++) {
					       if(grid.get(i).get(j) == 1) {
						         badCells.add(new Cell(i, j));
						   }
					  }
				}
				
				if(badCells.size() == 0) {
				      return -1;
				}
				
				for(int i=0; i<len; i++) {
				       for(int j=0; j<breadth; j++) {
					       if(grid.get(i).get(j) == 0) {
						         int min = Integer.MAX_VALUE:
								 for(Cell: c: badCells) {
								       min = Math.min(min, Math.abs(c.row - i) + Math.abs(c.col - j));	   
								 }
								 max = Math.max(max, min);
						   }
					   }
				}
				return max;
	  }
	  static class cell {
	        int row;
			int col;
			Cell(int row, int col) {
			       this.row = row;
				   this.col = col;
			}
	  }
}
Comments (1)