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)

- Anonymous October 14, 2018 | Flag Reply
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

- lee19856 October 15, 2018 | Flag Reply
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...

- lee19856 October 15, 2018 | Flag Reply
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];
    }
};


int findNodeWithDegree(int n, AdjList &adjList){
    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();
            for(auto &other : adjList.getAllNeighbors(front)){
                if(counts.find(other) == counts.end()){
                    q.push_back(other);
                }
                counts[other]++;
            }
        }
    };

    for(auto &a : adjList.nodes){
        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() {
    AdjList adj;
    adj.insert(0,1);
    adj.insert(0,2);
    adj.insert(0,3);
    adj.insert(1,2);
    adj.insert(1,4);
    adj.insert(1,5);
    adj.insert(4,5);
    adj.insert(2,4);
    adj.insert(2,5);
    adj.insert(2,3);
    adj.nodes[6] = vector<int>();

    cout << findNodeWithDegree(5,adj) << endl;
    return 0;
}

- Guy October 17, 2018 | Flag Reply
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;
}

- Saurabh October 17, 2018 | Flag Reply
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.

- sharadnailwal96 November 25, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

amazing

- EleciaHadley April 18, 2020 | 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