Amazon Interview Question for SDE1s


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 0 vote

In a grid?

- Anonymous January 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

recursive solution

public static int dofindNumOfWays(int [] [] matrix, int i ,int j,int value){
		if (i>=matrix.length||j>=matrix[0].length){
			return 0;
		}else if (matrix[i][j]==value){
			return 1;
		}else{
			int right = 0;
			int down = 0;
			int diagno = 0;
			right = dofindNumOfWays(matrix,i,j+1,value);
			diagno = dofindNumOfWays(matrix,i+1,j+1,value);
			down  = dofindNumOfWays(matrix,i+1,j,value);             
			return right + down + diagno ;
		}

}

- Scott January 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Grid can be viewed as strongly connected graph, so u can use djiktra Algorithm

- Siddharth January 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

dp solution

public static int findWaysDP (int [] []matrix , int x, int y){
		int row = matrix.length;
		int col = matrix[0].length;
		int [] [] dp = new int [row][col];
		dp [0][0] = 1;	
		for (int i = 0 ; i<row ;++i){
			for (int j = 0 ; j <col ;++j){
				// horizon
				if (j+1<col){
					dp [i][j+1] += dp[i][j];
				}
				//diagno 
				
				if (j+1<col &&i+1<row){
					dp [i+1][j+1] += dp[i][j];
				}
				
				
				//down
				if (i+1<row){
					dp [i+1][j]+= dp[i][j];
				}
				
			}
		}
		
		
		return dp [x][y];

}

- Scott January 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use wave algorithm

- glebstepanov1992 January 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about doing an A* search? The nodes (grid points in this case) are expanded based on the function f(x) = g(x) + h(x), where g(x) = path code till now to arrive at node (a,b), where 0<=a<x and 0<=b<y, and h(x) = the heuristic function which describes the estimated cost to get to (i,j) from (a,b). If the heuristic chosen is admissible and monotonic, the A* will give you the optimal path. We can use DP to compute h(a,b) and f(a,b).

- copyconstructor January 11, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More