## Amazon Interview Question

Interns**Country:**United States

**Interview Type:**Phone Interview

```
// ZoomBA
def careercup(path, x, y, M, N){
if ( x > M || y > N ) return 0
if ( x == M && y == N ) {
println( path )
return 1
}
result = ( careercup( path + str('(%s,%s)', x+1,y) , x + 1, y , M, N) +
careercup( path + str('(%s,%s)', x,y+1) , x, y + 1 , M, N) +
careercup( path + str('(%s,%s)', x+1,y+1) , x + 1, y + 1, M, N) )
}
println ( careercup('(0,0)', 0, 0, 2, 2) )
```

private static int findPaths(int i, int j){

int paths = 0;

calc++;

if (i == (m-1) && j == (n-1)) {

return 1;

}

calc++;

if(i+1 < m) {

paths += findPaths(i+1,j);

}

calc++;

if(j+1 < n) {

paths += findPaths(i,j+1);

}

calc++;

if(i+1 < m && j+1 < n) {

paths += findPaths(i+1,j+1);

}

return paths;

}

private static int findPathsOpt(int i, int j){

if (store[i][j] != 0) {

return store[i][j];

} else {

int paths = 0;

if (i == (m-1) && j == (n-1)) {

return 1;

}

if(i+1 < m) {

paths += findPathsOpt(i+1,j);

}

if(j+1 < n) {

paths += findPathsOpt(i,j+1);

}

if(i+1 < m && j+1 < n) {

paths += findPathsOpt(i+1,j+1);

}

store[i][j] = paths;

return paths;

}

}

```
#include <iostream>
#include <queue>
using namespace std;
int careerSolution(int m, int n)
{
int steps[m][n];
bool visited[m][n];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
steps[i][j] = 0;
visited[i][j] = false;
}
}
steps[0][0] = 1;
visited[0][0] = true;
class coord {
public:
coord(int lhs, int rhs)
{
x = lhs;
y = rhs;
}
coord operator=(coord& rhs){
this->x = rhs.x;
this->y = rhs.y;
}
int x;
int y;
};
coord start(0, 0);
queue<coord> q;
q.push(start);
int dx[3] = { 1, 0, 1 };
int dy[3] = { 0, 1, 1 };
while(!q.empty()){
coord current = q.front();
q.pop();
for(int i = 0; i < 3; i++){
coord next(current.x + dx[i], current.y + dy[i]);
if(next.x < m && next.y < n){
steps[next.x][next.y] += steps[current.x][current.y];
if(!visited[next.x][next.y]){
q.push(next);
visited[next.x][next.y] = true;
}
}
}
}
return steps[m-1][n-1];
}
```

```
class RobotMove{
int matrix[][];
int counter;
int size;
int width;
int height;
RobotMove(int n, int m){
matrix = new int[n][m];
counter = 0;
width = n;
height = m;
move(0,0,"0,0");
}
public void move(int n, int m, String path){
if (m == height-1 && n == width-1) {
System.out.println(path);
counter++;
}
if (n+1 <= width-1) move(n+1,m,path+"->"+(n+1)+","+m);
if (m+1 <= height-1) move(n,m+1,path+"->"+(n)+","+(m+1));
}
}
```

```
class RobotMove{
int matrix[][];
int counter;
int size;
int width;
int height;
RobotMove(int n, int m){
matrix = new int[n][m];
counter = 0;
width = n;
height = m;
move(0,0,"0,0");
}
public void move(int n, int m, String path){
if (m == height-1 && n == width-1) {
System.out.println(path);
counter++;
}
if (n+1 <= width-1) move(n+1,m,path+"->"+(n+1)+","+m);
if (m+1 <= height-1) move(n,m+1,path+"->"+(n)+","+(m+1));
}
}
```

Here is the foundational one :

- NoOne October 11, 2016math.stackexchange.com/questions/1422686/number-of-paths-in-a-mxn-matrix

Here is the code you need in normal language :

geeksforgeeks.org/print-all-possible-paths-from-top-left-to-bottom-right-of-a-mxn-matrix/