Amazon Interview Question for Software Engineer / Developers






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

The data structure is an adjacency matrix ( A ) .

To find a path of length k - A=A^k ( can be computed in logK time ).
if in A^k A[u][v]==1 then there exists a path of length k between u & v .

- LOLer July 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Loler: would u mind to clarify how it's possible in O(logk) time? The adjacency matrix need to be multiplied O(logk) times, but that leads to O(n^3 logk) time in total. Plz correct me. Thanks.

- Anonymous July 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

k-th power of a matrix (A^k) can be found in O(log k) time (called repeated doubling). If A[u][v]==1 in A^k ,then there exist a path of length k between u and v. Thus, in O(log k) time we can tell whether the given two nodes are connected by a path length of k.

- Anonymous July 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's O(log k) /matrix multiplies/, which is not, in general, O(log k) time.

- Anonymous July 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree. it's log k matrix multiplications which is not O(logk)time.

- Anonymous July 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

its log(k)*ncube

- Anonymous November 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Union-find ?

- Anonymous July 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Exactly....that's wat i thought at first....
but it will only satisfy the second case.
For first case it will not O(1)...since we have to test ( find(u)==v or find(v)==u ),one of them will be O(1)...but other is definitely not.

- XYZ July 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you elaborate how would use union find for second case? Thanks

- someone July 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

any solution for this problem?

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

the problem is find edge it can't be found in o(1) using above methods

- anonymous September 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

do a dfs on each node, make a map for each node with nodes to which it could be reached in number of steps

example
node a{
b: [1,3],
c: [3,4],
}
do it for each node and while querying we get all the required data by doing lookup in relevant node-maps

- Anonymous October 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Search for quick find and quick union

- Adi October 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't think in case that possible. The problem is related to Transitive closure, where a path of length K exists by OR Adj_1 || Adj_2 || .....|| Adj_k.

where Adj_1(i,j) = 1 in case i, j path exists 0 other wise.

And Ajd_2 = matrix multiplication of Adj_1 with itself .

- ekapil2 November 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

adjacency matrix with A(i,j) storing path length from i to j.

- coolpk November 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if paths with different length exist between two nodes?

- SwingMan December 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Then store all of them. You could use a linklist.

- ibread January 23, 2011 | Flag


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