## Symantec Interview Question for Principal Software Engineers

• 0

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?

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

Yes it is level 2 not one typo

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

Use BFS, and check the size of queue .

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;
}``````

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

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

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

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

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

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

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);``````

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

Replace the code in the above comment to:

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

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

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

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.

### 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.