Microsoft Interview Question for SDE1s


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
4
of 8 vote

It's O(1). Use a magic potion if needed to turn the enemy link into a friend link, and go directly from the start node to the end node. Neat trick question.

- Anonymous September 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Yes, good answer. downvoters are clueless...

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

How will your algo know that which path leads to end node? Thats where you have to traverse and complexity comes in

- sandy September 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I guess the nodes are not necessary to be neighbors.

- Alex September 29, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey you are right! This is about a complete graph:
*graph where every two nodes are either ...*
Kudos on the answer!

- grekogecko November 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If this is what interviewer is expecting in MS interview, he can as well ask a word from crossword puzzle which would be a better question.

- Vib April 19, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

O(E * log(V)) using Dijkstra Algorithm.

- Qi Lu September 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If there are unlimited magic potions then the complexity will be of Dijkstra’s algorithm. In case the number is fixed then it will need to be analyzed.

- kr.neerav September 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@kr.neerav If the cost of the shortest path from start node to end node is bigger than “magic potions”, there is no way to go to the end node.

- Qi Lu September 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nope, Qi is correct. I use nodes and I use A* (Dijkstra with a heuristic). You find path's by comparing the distances between neighboring nodes. You can give each node a value to determine who it's 'friends' are. At least, this is how I read the question.

- jdmdev October 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

It is a union-find problem. Balanced tree implementation takes O(lg N).

- Anonymous September 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Can you provide more details?
I have no idea how one could solve such problems better then linear time.

- Alex September 29, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the sketch of the solution which runs in O(E).
Let's call friend edges white and enemy edges black.
First we need to find all the connected components of the graph with white edges. We can run simple dfs or bfs for that. Now let's assume that a connected component in one vertex. And let's build new graph on this set of new vertices. The edges of this graph are black edges (more specifically if we have four vertices and white edges 1-2, 3,4 and black edges 2-3,1-2,1-4, we will first find that there are 2 components (1,2) and (3,4) and then the edge in the new graph will connect these 2 components). On the new graph we need the shortest path and check if it costs less than k.

Second solution, also O(E). We will use 0-1-bfs: bfs on the graph with edges of weights 0 and 1. You can read more about it in the net. Here we just use it to find the shortest path and check it to be less than k.

- Alex September 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

“every two nodes are either friends or enemies with each other”
There for, for any 2 nodes they are either friends or enemies.
If the two you need to get between are friends you are done
If they are enemies use your magic potion.
Obviously there is something else going on here.
Perhaps it should be reworded to state that some nodes are connected to other nodes via friend links and others are connected using enemies and some nodes are not directly connected to other nodes. I suppose you might also stipulate that all the nodes are indirectly linked in one way or another.
Then your task is to find a path from one node to another minimizing the number of enemy links you must traverse. Or perhaps the goal is to find the path that only uses a certain number of enemy links.
Or perhaps they are all interlinked and maybe the goal is to travel through all of the other nodes expending a minimum amount of potion though the first interpretation seems far more likely.

- DR A.D.M October 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its a Shortest Path Problem
Assign Friend edges length 0 and enemy edges length 1
find the shortest path and whenever you encounter enemy edge increase answer by 1

- Vamshikrishna October 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

my algo:

if(next node is a fried){
travel and add in stack
}

if(next node is enemy){
pop and go to previos friend node an calth the next path if there are more paths, else keep on poping to its previois fiend nodes)

eventually if there exists a path it will be found......


conplexity(worst case is n square)
best is (n log n)

- argho.chatterjee.001 September 18, 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