Starup Interview Question
SDE-2sCountry: India
Interview Type: Written Test
#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
class Connection {
public:
Connection(int from, int to)
{
from_ = from;
to_ = to;
}
int from_, to_;
};
string Topology(vector<Connection> const &a)
{
unordered_map<int, int> nodes;
for (auto c : a) {
if (++nodes[c.from_] > 2 ||
++nodes[c.to_] > 2)
{
return "star";
}
}
if (a.size() == nodes.size() - 1) {
return "bus";
}
if (a.size() == nodes.size()) {
return "ring";
}
return "something else";
}
int main()
{
cout << Topology({Connection(1, 2), Connection(2, 3)}) << "\n";
cout << Topology({Connection(1, 2), Connection(2, 3), Connection(3, 1)}) << "\n";
cout << Topology({Connection(2, 1), Connection(3, 1), Connection(4, 1), Connection(5, 1)}) << "\n";
return 0;
}
Hi,
Assumptions:
1. There will always be 2 arrays that will describe the connection and they will always be in the same length.
2. The arrays arranged in the correct order, the next pair of connection will be the continue of the previous.
For more professional way of detection I think that creating the structure of network in linked list data structure is a better way.
- ProTechMulti October 16, 2017