## Google Interview Question for SDE-2s

Country: United States

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

Traverse the graph with DFS\BFS
For each node check its degree (get the neighbors, if it is its own neighbor, i.e. cycle, add another one)

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

My finding..

1) the number of subtrees of a node is called degree of the node.
2) The degree of a tree is the maximum degree of a node in the tree.
3) A binary tree is degree 2.

then i would to this

1) review all subtree of each node with BFS
2) have one variable to keep updating max num of subtree
3) once complete to review all nodes, then return max num which would be *degree* of the graph or tree

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

@ shivamdurani220 - BTW, are you sure if these are really interview questions from Google interview? recently you uploaded many and looks like some of them are exactly same as one in leetcode.. i'm not trying to be rude but just wondering...

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

``````struct AdjList{
unordered_map<int,vector<int> > nodes;

void insert(int i, int j){
nodes[i].push_back(j);
nodes[j].push_back(i);
}

vector<int> &getAllNeighbors(int n){
return nodes[n];
}
};

unordered_map<int,int> counts;

auto bfs = [&](int node){
deque<int> q;
counts[node] = 0;
q.push_back(node);

while(!q.empty()){
auto front = q.front();
q.pop_front();
if(counts.find(other) == counts.end()){
q.push_back(other);
}
counts[other]++;
}
}
};

if(counts.find(a.first) == counts.end()){
bfs(a.first);
}
}

for(auto &p  : counts){
if(p.second == n){
return p.first;
}
}

return -1;
}

int main() {

return 0;
}``````

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

Though the code to calculate vertex degree depends on the implementation of Graph data structure, here is a simple method to calculate a degree of a vertex.

We return TRUE if any of the vertex has a degree equal to N.

``````public static int degree(Graph G, int v)
{
int degree = 0;
for (int w : G.adj(v)) degree++;
return degree;
}``````

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

we can make a adjency list and then we can count for each node and say how many nodes have n degree.

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

amazing

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.