Symantec Interview Question for Principal Software Engineers


Country: United States




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

Wouldn't that be level 2 in your example, with 5 nodes?

- 010010.bin August 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes it is level 2 not one typo

- kaushikjp August 03, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use BFS, and check the size of queue .

- pabhi August 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// returns the max number of nodes in any level
int levelWithMostNodes( Node *root )
{
	if( !root ) // bork out...

	queue<Node *> Q;

	int currNumInLevel = 0;
	int maxNodesAnyLevel = 0;

	Q.enqueue(root);
	Q.enqueue(NULL); // NULL is a sentinel denoting end of level

	while ( ! Q.empty() )
	{
		root = Q.dequeue();

		// if we pulled off sentinel, we finished a level
		if ( root == NULL )
		{
			if ( currNumInLevel > maxNodesAnyLevel ) {
				maxNodeAnyLevel = currNumInLevel;
			}
			if ( !Q.empty() ) Q.enqueue(NULL); //more level(s) to delimit

			// reset this as new level starts with next dequeue
			currNumInLevel=0;

			continue;
		}
		
		// an actual node was pulled off...
		currNumInLevel++;
		
		// add children if they exist
		if ( root->left ) Q.enqueue(root->left);
		if ( root->right ) Q.enqueue(root->right);
	}
	
	return maxNodesAnyLevel;
}

- RecruitingForSandvineCanada August 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is not binary tree. Let me update question with sample node class.

- kaushikjp August 05, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

class Node
{
public:
const vector<Node*>& get_children() {
return children;
}

Node *add_child() {
Node* new_node = new Node();
children.push_back(new_node);
return new_node;
}
private:
vector<Node*> children;
};

- kaushikjp August 05, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If it's not a binary tree, just modify this part in the obvious way:

// add children if they exist
		if ( root->left ) Q.enqueue(root->left);
		if ( root->right ) Q.enqueue(root->right);

- RecruitingForSandvineCanada August 05, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Replace the code in the above comment to:

for(auto a:root->get_children()){
Q.enqueue(a);
}

- derpherplerpberpferp September 03, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

for(auto a:root.getChildren())
		Q.enqueue(a);

- deeeeeeeeeeeerp September 03, 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