unknown Interview Question for Software Engineers


Country: United States




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

public boolean isValid(List<TreeNode> nodes){
	  	HashSet<TreeNode> children = new HashSet<> ();
	  	// child node only has one parent node
	  	for (TreeNode node : nodes) {
	  		if (node.left != null) {
	  			if (!children.add(node.left)) return false ;
	  		}
	  		if (node.right != null) {
	  			if (!children.add(node.right)) return false ;
	  		}
	  	}
	  	
	  	TreeNode start = null ;
	  	int count = 0 ;
	  	for (TreeNode node : nodes) {
	  		if (!children.contains(node)) {	  			
	  			start = node ;
	  			count ++ ;
	  		}
	  	}	  	
	  	// only has one root node
	  	if (count > 1) return false ;
	  		
	  	// running bfs to make sure all nodes can be constructed as a binary tree 
	  	Queue<TreeNode> q = new LinkedList<> ();
	  	q.add(start) ;	  	
	  	while (!q.isEmpty()) {
	  	   int size = q.size() ;
	  	   for (int i = 0 ; i < size ; ++i) {
	  		   TreeNode cur = q.poll() ;
	  		   if (cur.left != null) {
	  			   q.add(cur.left) ;
	  			   children.remove(cur.left) ;
	  		   }
	  		   if (cur.right != null) {
	  			 q.add(cur.right) ;
	  			 children.remove(cur.right) ;
	  		   }
	  	   }
	  	}	  		  	
	  	return children.size() == 0 ;
	}

- Scott July 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why is the bfs check required?

- sumitjainn24 June 20, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We may want to also return if count==0. I also doubt if we need the bfs check.

- Luan June 21, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm pretty sure all you would need to do is have a simple map counting in_degrees. Then double check that every node has only 1 indegree except for 1 node that has 0 because it's the root.

- jbaum517 June 24, 2020 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

The solution can be designed with the following idea which runs in multiple passes:

Pass1: Read every node and for each node remember what other nodes are pointing to it using additional data structures

Pass2: Read every node to find the following:
a. Nodes who are pointed to by 0 other nodes ==> These are potential roots
b. Nodes who are pointed to by 1 nodes
c. Nodes who are pointed to by > 1 nodes

We have a valid binary tree iff:

1. The number of nodes whom nobody points to is 1 and that is the root
2. Every node is pointed to by at most one node
3. Starting with the root, and doing a DFS or a BFS covers all the nodes in the list

- Santosh July 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why is the 3rd check required if 1 and 2 are fulfilled?

- sumitjainn24 June 20, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution can be designed with the following idea which runs in multiple passes:

Pass1: Read every node and for each node remember what other nodes are pointing to it using additional data structures

Pass2: Read every node to find the following:
a. Nodes who are pointed to by 0 other nodes ==> These are potential roots
b. Nodes who are pointed to by 1 nodes
c. Nodes who are pointed to by > 1 nodes

We have a valid binary tree iff:

1. The number of nodes whom nobody points to is 1 and that is the root
2. Every node is pointed to by at most one node
3. Starting with the root, and doing a DFS or a BFS covers all the nodes in the list

- Santosh July 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Find the heights of the all the node's subtree. We will get the node whose subtree is of max height. If we want a valid subtree, then all the other nodes should come in this max height tree.

- Sandeep Rastogi July 24, 2015 | 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