Wildfire - Amazon Top Interview Questions
Problem Statement :
You are given a two-dimensional integer matrix representing a forest where a cell is: 0 if it's empty 1 if it's a tree 2 if it's a tree on fire Every day, a tree catches fire if there is an adjacent (top, down, left, right) tree that's also on fire. Return the number of days it would take for every tree to be on fire. If it's not possible, return -1. Constraints 0 ≤ n * m ≤ 200,000 where n and m are the number of rows and columns in matrix Example 1 Input matrix = [ [1, 1, 1], [1, 2, 1], [1, 1, 1] ] Output 2 Explanation On the first day fire will spread to everywhere except the corner trees and then the next day they will spread to the corner trees. Example 2 Input matrix = [ [1, 1, 1], [0, 0, 0], [1, 2, 1] ] Output -1 Explanation Fire on bottom row can't move across the middle row because it's empty.
Solution :
Solution in C++ :
int solve(vector<vector<int>>& matrix) {
int n = matrix.size();
if (n == 0) return 0;
int m = matrix[0].size();
int total = 0;
queue<pair<int, int>> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] != 0) total++;
if (matrix[i][j] == 2) q.push({i, j});
}
}
int dir[5] = {0, -1, 0, 1, 0};
int cnt = 0, days = 0;
while (!q.empty()) {
int sz = q.size();
cnt += sz;
for (int j = 0; j < sz; j++) {
int cx = q.front().first;
int cy = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int nx = cx + dir[i];
int ny = cy + dir[i + 1];
if (nx < 0 || ny < 0 || nx >= n || ny >= m || matrix[nx][ny] != 1) continue;
matrix[nx][ny] = 2;
q.push({nx, ny});
}
}
if (!q.empty()) days++;
}
return (cnt == total) ? days : -1;
}
// TC :O(N*M), SC :O(N*M)
Solution in Python :
class Solution:
def solve(self, matrix):
# basic check
if matrix == []:
return 0
# initialization of the variables
n, m, dist = len(matrix), len(matrix[0]), 0
# filling the queue with all the trees which are already on fire
d = deque((i, j, dist) for i in range(n) for j in range(m) if matrix[i][j] == 2)
dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)]
# performing bfs
while d:
i, j, dist = d.popleft()
for x, y in dirs:
r, c = i + x, j + y
if 0 <= r < n and 0 <= c < m and matrix[r][c] == 1:
d.append((r, c, dist + 1))
# print('burnt matrix[{}][{}]'.format(r,c),'on day ',dist+1)
matrix[r][c] = 0
# checking if any tree is left unburnt
for i in range(n):
for j in range(m):
if matrix[i][j] == 1:
return -1
return dist
View More Similar Problems
Find Merge Point of Two Lists
This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share
View Solution →Inserting a Node Into a Sorted Doubly Linked List
Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function
View Solution →Reverse a doubly linked list
This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.
View Solution →Tree: Preorder Traversal
Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's
View Solution →Tree: Postorder Traversal
Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the
View Solution →Tree: Inorder Traversal
In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func
View Solution →