Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

I suggest to use union find disjoints sets and run two times...then when i would like to know if 2 nodes are connected just access to pset and check if have same numbers.... fo example, i have this graph:
1->2->3
4->5
UFDS:
first time
index: 1 2 3 4 5
element(pset): 2 2 3 5 5

second time
index: 1 2 3 4 5
element(pset): 3 3 3 5 5

3 and 4 are connected ? 3 and 5 are equal ? No.
1 and 2 are connected ? 3 and 3 are equal ? Yes.

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

Can't you run DFS/BFS once and store the connectivity information as an index, say?

Next time the connectivity query will be a constant time operation of just checking the corresponding indices for equality.

- n.j.joshi January 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If two vertexes are connected in Directed graph, it means that they belongs to the same Strongly connected component. You can find all SCC by DFS in O(n).
Traverse graph by dfs and save enter in(v) and exit time out(v) for each of them. Then invert all edges(actually transpose adjecency matrix) and make dfs again, but when you in vertex v, you should to choose first those vertex that has maximum out time. So all distinct trees of dfs will be strongly connected components.
So you have preprocess the graph, so then all you need it is just to check whether both vertexes belong to the same SCC, it can be done in O(1).

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

1->2->3 
4->5 
UFDS: 
first time 
index: 1 2 3 4 5 
element(pset): 2 2 3 5 5 

second time 
index: 1 2 3 4 5 
element(pset): 3 3 3 5 5

- nodes January 23, 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