Amazon Interview Question for SDE-2s


Country: India
Interview Type: In-Person




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

This sounds like a shortest distance algorthm to me. Assuming the snake and ladder board can be represented as a map, just use breadth-first search.

1. Get all next hop from starting/current position, store them in a queue
2. Pop the queue, get all next hop from this position, enqueue them
3. Repeat step 2 until we find the finish position. The number of repetition is the number of shortest jump

- gtan August 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

When getting the next hop, have to make sure the postition hasn't been visited

- gtan August 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

In short, do a breadth first search! However, successor is a bottom of a ladder, empty queue and enqueue next successors. If it is a snake-mouth, do not expand!

- Damith August 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Create an array for all the numbers on the board 1-100. Go through the array in order, keeping track of all the jumps in a sorted fashion. Once you have this, discard the first array to free up space. Select the biggest jump, and iterate again to get to this biggest jump in best fashion (0-StartPoint1) (EndPoint1-100). Create two arrays in sorted fashion to get the next two biggest jumps. Iterate like this till you get the series of biggest jumps needed to finish.

- Saurabh Arora August 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This problem is similar to frog jump in an array.But before that we will have to model the problem in that format.

Create an array of size 100 and for each position store 6 if there is no snake or ladder. store jump count . if ladder is present at that point . If snake is present then store -ve jump at that location.

Now we have to solve in how many minimum steps we can reach till end.

Main problem can be solved using dynamic programing in O(n^2) time complexity and O(n ) space. tech-queries.blogspot.com/2011/08/find-optimal-number-of-jumps-to-reach.html

- OTR August 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1> greedy approach: posion=posion+6 if there is no ladder between [posion, posion+6] and 
2>  posion+6 != snake position, i.e no negative weight. else posion=ladderTop
DP: 2nd approach

- Ashis Kumar Chanda August 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if there are two ladders in reachable six steps, first one takes 10 steps jump and second one 15 steps jump.? Is it being handled here?

- OTR August 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

GREEDY Is a Failure Approach Here Man!..Consider only two ladders , 4->43 , and 22->100 !!! .... Use as Weighted DAGs instead!..and calculate the Shortest Path from node 1 to node 100 ...

Dp APPROACH would work , if we move the other Way round , like # of Jumps to reach 100 is MIN((1+MIN(#of jumps for 99,98,97,96,95,94)),LADDERtoNODE[100])....
Base Case would be reaching nodes 1,2,3,4,5,6,7 ...

- Dale Steyn August 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Consider the game as Directed Graph where every node has a directed edge of cost 1 to it's next 6 nodes . In addition a directed edge of cost 0 from bottom of ladder to top of ladder and directed edge of cost zero from snake's mouth to it's tail.

2. After this initialization apply single source shortest path (Bellman Ford) from 1 for 100.

the cost will be number of dice rolls.

Please comment if i missed some thing.

- VIJAY TRIPATHI August 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"Snake mouth to tail of cost zero" has some problem it seems. It is not optional to go to tail. Am i correct?

- Srujan October 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The snake board can be represented as an augmented linkedList Node
Class Node
{
Next ;
Random ;
}

Base case for back tracking is first if the prev node value is greater than the next node
if reached destination return count.

- Rohit August 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Class MyClass{

int positon=0;
public int getCurrentPosition{

position+= diceResult();

if(isLadderAvailable(postion)){

getCurrrentPostion();

if(postion==100)
break;
}


public int diceResult()
{
// throw the dice and return the results
}

boolean isLadderAvailable(int postion)
{
// check if the ladder is availale at the position given or not
}
}

- Pawan August 29, 2013 | 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