Microsoft Interview Question for Software Engineer / Developers






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

Good move, Peter!

I think this is the best you can do as far as run-time complexity. It's linear time and linear memory. You can't do better than linear time.

Let's assume that it's a COMPLETE BINARY TREE.

BFS - we have to maintain a QUEUE and the algorithm is iterative. The largest this QUEUE would ever get is 2^(H-1) (H is the height of the tree), which is the last level in the binary tree.

DFS - It can be implemented Iterative or Recursive. If we implement it Iteratively, we need a STACK and at most H elements are in the stack at any given time.

For COMPLETE BINARY TREE, DFS is better. We can generalize this into a B-Tree.

If the tree is deformed and becomes a something like a linked list, BFS is better.

- Khoa November 06, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If m > n, use DFS. Otherwise, use BFS.

- Eyal March 11, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It says: "There are two large tree (not binary)". They are NOT binary.

- Anonymous June 14, 2008 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

As mentioned in question tree is not binary tree, if we try to insert into second tree then search complexity will increase.

Use the DFS search algorithm as space complexity is much lesser than BFS.

As Joe mentioned:
By using DFS traverse first tree and create a hash table, now while traversing the second tree look into hash table.

- Deepak Garg May 27, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use BFS to traverse the first tree and put the data as a key to a hash table with 0 in the value part. Then traverse the second tree and look through the hash table if the data already exists in the table. If found make the value part as 1 iff 0 and increment your counter. If the value part is 1 then do nothing.

I am wondering which algo BFS/DFS will be efficient here.

Any coments/suggestions are most welcome.

- Joe November 06, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Take the second tree and try to insert that node in the second tree.. If element matching to that is found then don't insert and u just found similar element. Mark the node in first tree as visited. OR maintain a hash table of visited nodes. Even an array can be maintained.

- KSD October 20, 2007 | 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