Amazon Interview Question for SDE-2s


Country: India




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

1. Build graph of reachable stones from every stone
2. Find shortest path in graph using dijkstra algorithm.

Here is my solution

def min_rabbit_jumps(js, rs):
    """Find minimum number of jumps to cross river

    js - possible jumps
    rs - distance between adjacent rocks starting from first

    >>> min_rabbit_jumps([3,1], [1,2,1])
    2
    >>> min_rabbit_jumps([3,1], [2,1,2])
    3
    >>> min_rabbit_jumps([3,1], [1,4])
    inf
    """
    ns = to_graph(js, rs)  # index -> children

    # using dijkstra algorithm find shortest path in graph `ns`
    i2d = {i: float("inf") for i in range(len(ns))}
    i2d[0] = 0
    q = set(range(len(ns))) # unprocessed nodes queue
    while q:
        # find closest elemnt
        d, i = min((i2d[i], i) for i in q)
        q.remove(i)

        for n in ns[i]:
            if i2d[n] > d + 1:
                i2d[n] = d + 1
    return i2d[len(rs)]


def to_graph(js, rs):
    """Convert jumps `js` and rocks `rs` to graph

    Returns graph of possible positions and available jumps from them.
    """
    # index to distance and vice versa
    d, i2d, d2i  = 0, {0: 0}, {0: 0}
    for i, r in enumerate(rs):
        d += r
        i2d[i + 1] = d
        d2i[d] = i + 1

    # nodes of the graph
    ns = [[] for _ in i2d]
    for i, d in i2d.items():
        for j in js:
            ji = d2i.get(d + j) # jump index
            if ji is not None:
                ns[i].append(ji)
                ns[ji].append(i)
    return ns

- Pavel Aslanov July 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. represent the rocks in an array rocks [n]( where n= (m2-m1) + (m3-m2).......) such that rocks[i]=1 if a rock is present at i distance from m1 else
2. Take another array jumps[n]. Initialize all jumps[i]= infinity and jumps[0]=0
3. now start
from i=1 to i=n
............if(rocks [i] != 0)
.................. jumps[i]= min( jumps[i-Wj]) where Wj is the array containing Ways to jump
............else
...................jumps[i]= infinity
4. jumps[n] contains your minimum jumps

- Sugarcane_farmer June 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the rabbit can jump backward, so if you use dp,
jumps[i]=min(jumps[i-w[j]],jumps[i+w[j]]);
but at this time jumps[i+w[j]] has not calculated yet

- Anonymous June 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How rabbit can perfrom jump of length 3 if N=2.

- gdg June 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think N here means there are N types of jumps the rabbit can perform.

So in the example, N = 2 and the jump lengths rabbit can perform are 3 and 1.
If N = 3, then maybe the rabbit can perform a jump of length 4
...

- Jason June 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, I mean additionally a jump of length 4 besides 3 and 1 when N = 3

- Jason June 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if N=3 its not necessary the length of jump would be 4. It can be anything.

- k.87.sharma June 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Let A be the array for storing results which has been initialized to a highest integer value, Let D be the array for storing diff, eg. D[1] contains m2-m1 and so on. Let R contains the list of possible values where rabbit can jump.

for(i=1;i<N;i++) {
  diff =0;
  for(j=i;j>0;j--) {
    diff+=D[j];
    if(R.exists(diff)) {
      if(A[i]>A[j]+1) {//careful about the overflow
        A[i]=A[j]+1;
      }
    }
  }
}

And A[N-1] contains the minimum value. :)

- viola August 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You actually don't need Djisktra for this. The Length of the Jump doesn't need to be optimized, just the number of them.

We can create a graph, with edge I->J abd J<-I if j can be reached from I based on the number of Steps between I and J and what is allowed. And then we just find the shortest path from S - E in terms of number of nodes using BFS (Mark visited node, else will fail).

I am still wondering on the reason for the "can also jump backwards", if the number of jump lengths possible backwards is the same as forwards, I don't see how a shortest path will have a jump backwards. Even if the Jump backwards were different than forwards, I don't see how a shortest path will include a cycle. Added to confuse ?

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

try below test case to understand "can also jump backwards"

Input
For each test case, the first line contains 2 space separated integers, M and N. The second line contains M-1 space separated integers denoting the distance between consecutive rocks in the river. The third line contains N space separated integers denoting the possible jump lengths.
4 3
12 3 2
15 5 3

- Rajesh yadav October 15, 2016 | 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