Interview Question for Software Engineers


Country: United States
Interview Type: Written Test




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

"Integer array with dimensions M and N" is a matrix so can be assimilated to a Graph (G).

1. Use BFS, with P as entry vertex and stop when you find Q. During this phase, keep a track of the parents of each vertex. (may a HashMap<Vertex,Parent>)
2. Starting from Q, ascend all the parents until P adding one parent by one to a List.

Time complexity O(Vertices+Edges) + O(Depth of G).
Space complexity : O(Vertices) + O(Depth of G).

- GT March 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

So your answer is pretty close, but your BFS does not guarantee shortest path (only a path). Instead of a simple BFS, use Dijkstra's algorithm with P as the source. Like you said, maintain the parents in a hashmap or array of some sort and then ascend through the parents starting with Q's parent to determine the shortest path.

- akyker20 March 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How would BFS not guarantee the shortest path? Are you thinking there are weights?

- tjcbs2 March 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am wrong... Sorry about that. I confused BFS with DFS. Rookie mistake. I believe your answer is good.

- akyker20 March 16, 2015 | 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