Amazon student Interview Question for Students


Country: Germany
Interview Type: Phone Interview




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

private int getPath(int start, int end, boolean[] vis, int[] pathLen)
{
    if(start == end) return 0;
    if(pathLen[start] != -1) return pathLen[start];
    
    int len = m_Matrix.length + 1;

    vis[start] = true;
    for(int i = 0; i < m_Matrix.length; ++i){
        if(vis[m_Matrix[start][i]]) continue;
        len = Math.min(len, 1 + getPath(i, end, vis, pathLen));
    }
    vis[start] = false;

    return pathLen[start] = len;
}
public int shortestPath(int start, int end)
{
    if(start == end) return 0;
    if(m_Matrix[start][end]) return 1;
    
    boolean[] vis = new boolean[m_Matrix.length];
    Arrays.fill(vis, false);
    int pathLen = new int[m_Matrix.length];
    Arrays.fill(pathLen, -1);

    int len = getPath(start, end, vis);
    return len > m_Matrix.length ? -1 : len;
}

- uuuouou March 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

shouldn't we get a pair of ints for start, and another pair for end?

- enriquevr March 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

by adding int[] parent, you can run BFS from the start. Set all nodes' parent to -infinity, and start to 0. Whenever you visit a node because it is adjacent to another already visited node and its parent is not set, set the parent to the current node.

At the end, by finding end's parent, end's parent's parent's , .... you have the path backward. Reverse it.

- mahdi.oraei March 04, 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