Microsoft | Remote Onsite | Module Loading Sequence

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2022-01-24Added By Anonymous
Source: Link
Microsoft | Remote Onsite | Module Loading Sequence Problem: Given dependecies of modules on other modules as a array of arrays. eg: Arr[i] = {a,b,c} => module 'i' depends on modules 'a', 'b', and 'c' Before module 'i' can be loaded, modules 'a' , 'b' and 'c' shall be loaded. For simplicity assume that all modules are represeted with +ve integers. Write a function to return a list of all dependencies of a given module in a sequential order. vector<int> load(vector<vector<int>> dependencies, int module) {} eg: Given Input: 1 : 2, 3 2 : 5 3 : 4, 5 4 : 5 : for module 1: output shall be : 5,4,3,2,1 or 4,5,3,2,1 for module 2: output shall be : 5,2 for module 4: output shall be : 4 for module 8: output shall be : 8 //(Note 8 is not present in the input array of arrays) Return an empty array if given input contain cyclic dependencies I couldn't solve it during interview. Later I wrote below code. Please review/suggest corrections. ``` /* From the given graph, we need to construct the portion of the transpose graph that is of interest with respect to the module whose dependeny list is asked for. That particular module shall be the sinking node in our graph. So first step is to create the portion of the transpose graph that is of interest by running a bfs over the given graph. (Note: We cann't just loop over the given input to create the transpose portion, because that way we will end up creating complete transpose, whereas we are interested only in a posrtion of it. Running topo sort on complete transpose won't give the desired result) Then do a toposort(dfs) over the resulted graph to find the result. */ #include <bits/stdc++.h> using namespace std; map<int, vector<int>> getTransposeOfInterest(vector<vector<int>>&dependencies, int module){ int n = dependencies.size(); map<int, vector<int>>hm; queue<int>q; vector<bool> visited(n, false); q.push(module); visited[module] = true; while(!q.empty()){ int sz = q.size(); while(sz){ sz -= 1; int m = q.front(); q.pop(); for(int d:dependencies[m]){ if(!visited[d]){ visited[d] = true; q.push(d); } hm[d].push_back(m); } } } return hm; } /* will return true if cycle is detetected */ bool topoSort(map<int, vector<int>>&graph, int root, map<int,int>&visit, vector<int>&depend_list){ visit[root] = 1; // mark visiting bool cycle = false; for(int v:graph[root]){ if(1 == visit[v]){ return true; // found a back edge,,, its a cycle } if(0 == visit[v]){ // first time discoverd node cycle = topoSort(graph, v, visit, depend_list); if(cycle){ break; } } } if(!cycle){ depend_list.push_back(root); visit[root] = 2; // mark visited } return cycle; } void solve_load(vector<vector<int>>&dependencies, int module){ if((module<0) || (module>=dependencies.size()) || (dependencies[module].empty())){ cout << module; return; } map<int, vector<int>>graph = getTransposeOfInterest(dependencies, module); vector<int>depend_list; map<int,int>visit; for(auto p:graph){ visit[p.first] = 0; } for(auto p:visit){ if(0 == p.second){ bool cycle = topoSort(graph, p.first, visit, depend_list); if(cycle){ cout << "Detected Cyclic Dependency" << endl; depend_list.clear(); break; } } } reverse(depend_list.begin(), depend_list.end()); for(int i:depend_list){ cout << i << " "; } } int main() { int m,n; m = n = 0; cin >> m; vector<vector<int>>dependencies(m); for(int i = 0; i<m; ++i){ cin >> n; for(int j = 0; j<n; ++j){ int x; cin >> x; dependencies[i].push_back(x); } } cin >> n; solve_load(dependencies, n); return 0; } ```
Comments