# Wildfire Sequel- Amazon Top Interview Questions

### Problem Statement :

```You are given a two-dimensional integer matrix matrix where

0 represents empty cell
1 represents a person
2 represents fire
3 represents a wall
You can assume there's only one person and in each turn fire expands in all four directions although fire can't expand through walls.

Return whether the person can move to either the top left corner or the bottom right corner. In each turn, the person moves first, then the fire expands. If the person makes it to either square as the same time as the fire, then they're safe.

Note that if you go a the square and then the fire expands in the same turn to the same square, you still survive.

Constraints

1 ≤ n, m < 250 where n and m are the number of rows and columns in matrix

Example 1

Input

matrix = [
[0, 0, 0],
[0, 1, 0],
[0, 2, 0]
]

Output

True

Explanation

The person can get to the top left corner.

Example 2

Input

matrix = [
[0, 2, 0],
[0, 1, 0],
[0, 2, 0]
]

Output

False

Explanation

There's fire getting in the way of the top left and bottom right corners.```

### Solution :

```                        ```Solution in C++ :

bool solve(vector<vector<int>>& matrix) {
int a, b, x, y, e, n = matrix.size(), m = matrix[0].size();

queue<pair<pair<int, int>, int>> bfs;
vector<int> dir{1, 0, -1, 0, 1};

for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[0].size(); j++) {
if (matrix[i][j] == 1) {
bfs.push({{i, j}, matrix[i][j]});
}
}
}

for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[0].size(); j++) {
if (matrix[i][j] == 2) {
bfs.push({{i, j}, matrix[i][j]});
}
}
}

while (!bfs.empty()) {
int s = bfs.size();

for (int i = 0; i < s; i++) {
a = bfs.front().first.first;
b = bfs.front().first.second;
e = bfs.front().second;

if (a == 0 && b == 0 && e == 1) {
return true;
}
if (a == n - 1 && b == m - 1 && e == 1) {
return true;
}

for (int j = 0; j < 4; j++) {
x = a + dir[j];
y = b + dir[j + 1];

if (x < 0 || y < 0 || x == matrix.size() || y == matrix[0].size()) {
continue;
}
if (matrix[x][y] == 3 || matrix[x][y] == 2) {
continue;
}

if (e == 1) {
if (matrix[x][y] == -1) {
continue;
}
if (x == 0 && y == 0 && matrix[i][j] != 2) {
return true;
}
if (x == n - 1 && y == m - 1 && matrix[i][j] != 2) {
return true;
}
matrix[x][y] = -1;
bfs.push({{x, y}, 1});
}
if (e == 2) {
matrix[x][y] = 2;
bfs.push({{x, y}, 2});
}
}

bfs.pop();
}
}

return false;
}```
```

```                        ```Solution in Java :

import java.util.*;

class Solution {
static int N;
static int M;
static int[][] matrix;
static int[][] fire;
static int[][] escape;
public boolean solve(int[][] m) {
matrix = m;
N = matrix.length;
M = matrix[0].length;
fire = floodfill(2);
escape = floodfill(1);
return (works(0, 0) || works(N - 1, M - 1));
}

public static boolean works(int r, int c) {
return (escape[r][c] < Integer.MAX_VALUE && escape[r][c] <= fire[r][c]);
}

public static int[][] floodfill(int root) {
int[][] time = new int[N][M];
for (int i = 0; i < N; i++) Arrays.fill(time[i], Integer.MAX_VALUE);
ArrayDeque<int[]> bfs = new ArrayDeque<int[]>();
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (matrix[i][j] == root) {
time[i][j] = 0;
}
}
}
int[][] dirs = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
while (!bfs.isEmpty()) {
int[] cell = bfs.pollFirst();
for (int[] dir : dirs) {
int newR = cell[0] + dir[0];
int newC = cell[1] + dir[1];
if (newR >= 0 && newC >= 0 && newR < N && newC < M && matrix[newR][newC] != 3
&& time[newR][newC] == Integer.MAX_VALUE) {
time[newR][newC] = time[cell[0]][cell[1]] + 1;
}
}
}
return time;
}
}```
```

```                        ```Solution in Python :

class Solution:
def solve(self, A):
INF = int(1e9)
R, C = len(A), len(A[0])

def neighbors(r, c):
for nr, nc in [[r - 1, c], [r, c - 1], [r + 1, c], [r, c + 1]]:
if 0 <= nr < R and 0 <= nc < C and A[nr][nc] != 3:
yield nr, nc

def bfs(queue) -> dict:
dist = {node: 0 for node in queue}
while queue:
node = queue.popleft()
for nei in neighbors(*node):
if nei not in dist:
dist[nei] = dist[node] + 1
queue.append(nei)
return dist

qfire = collections.deque()
qperson = collections.deque()
for r, row in enumerate(A):
for c, v in enumerate(row):
if v == 1:
qperson.append((r, c))
elif v == 2:
qfire.append((r, c))

dist_fire = bfs(qfire)
dist_person = bfs(qperson)
for place in ((0, 0), (R - 1, C - 1)):
if dist_fire.get(place, INF) >= dist_person.get(place, 2 * INF):
return True
return False```
```

## Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

## Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

## Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

## Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

## Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

## Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and