# Longest Increasing Path - Amazon Top Interview Questions

### Problem Statement :

```Given a two-dimensional integer matrix, find the length of the longest strictly increasing path. You can move up, down, left, or right.

Constraints

n, m ≤ 500 where n and m are the number of rows and columns in matrix

Example 1

Input

matrix = [
[1, 3, 5],
[0, 4, 6],
[2, 2, 9]
]

Output

6

Explanation

The longest path is [0, 1, 3, 5, 6, 9]```

### Solution :

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

const int dir = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
int dfs(const vector<vector<int>> &matrix, vector<vector<int>> &dp, const int &r, const int &c) {
if (dp[r][c] != -1) return dp[r][c];
dp[r][c] = 1;
for (int i = 0; i < 4; i++) {
int nextR = r + dir[i];
int nextC = c + dir[i];
if (nextR < 0 || nextC < 0 || nextR >= matrix.size() || nextC >= matrix.size() ||
matrix[nextR][nextC] <= matrix[r][c]) {
continue;
}
dp[r][c] = max(dp[r][c], 1 + dfs(matrix, dp, nextR, nextC));
}
return dp[r][c];
}
int solve(vector<vector<int>> &matrix) {
int m = matrix.size();
if (m < 1) return 0;
int n = matrix.size();
vector<vector<int>> dp(m, vector<int>(n, -1));
int ans = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (dp[i][j] == -1) dfs(matrix, dp, i, j);
ans = max(ans, dp[i][j]);
}
}
return ans;
}```
```

```                        ```Solution in Java :

import java.util.*;

class Solution {
private int row;
private int col;
private int[][] dp;
private boolean[][] visited;
private int maxLen = 1;
public int solve(int[][] matrix) {
if (matrix == null || matrix.length == 0)
return 0;
row = matrix.length;
col = matrix.length;
dp = new int[row][col];

for (int[] row : dp) Arrays.fill(row, -1);

for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
maxLen = Math.max(maxLen, dfs(i, j, matrix, -1));
}
}
return maxLen;
}

private int dfs(int i, int j, int[][] matrix, int len) {
if (i < 0 || j < 0 || i >= row || j >= col)
return 0;

if (matrix[i][j] <= len)
return 0;

if (dp[i][j] != -1)
return dp[i][j];

int l1 = dfs(i, j + 1, matrix, matrix[i][j]);
int l2 = dfs(i, j - 1, matrix, matrix[i][j]);
int l3 = dfs(i - 1, j, matrix, matrix[i][j]);
int l4 = dfs(i + 1, j, matrix, matrix[i][j]);

return dp[i][j] = 1 + Math.max(Math.max(l1, l2), Math.max(l3, l4));
}
}```
```

```                        ```Solution in Python :

class Solution:
def solve(self, a):
if not a or not a:
return 0
n, m = len(a), len(a)
dp = [[1 for j in range(m)] for i in range(n)]
for _, i, j in sorted((a[i][j], i, j) for i in range(n) for j in range(m)):
for ni, nj in ((i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)):
if 0 <= ni < n and 0 <= nj < m and a[i][j] < a[ni][nj]:
dp[ni][nj] = max(dp[ni][nj], dp[i][j] + 1)
return max(map(max, dp))```
```

## 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

## Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

## Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

## Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

## Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

## Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t