## Starup Interview Question for SDE-2s

Country: India
Interview Type: Written Test

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

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.

``````package com.cracking.starup;

public class DetectNetwrokTopology {

public static enum TopologyType {
BUS("Bus"),
CIRCULAR("Circular"),
STAR("Star"),
UNKNOWN("Unknown");

TopologyType(String description) {
this.description = description;
}

public String description;
}

public static final int[] srcNodes = {2,3,4,5};
public static final int[] destNodes = {1,1,1,1};

public static void main(String[] args) {
TopologyType type = getTopolgyType(srcNodes, destNodes);
System.out.println("Topolgy = " + type.description);
}

public static TopologyType getTopolgyType(int[] srcNodes, int[] destNodes) {

}

public static boolean isTopologyBus(int[] srcNodes, int[] destNodes) {
for(int i=0;i<srcNodes.length && i<destNodes.length; i++) {
if( (i+1)<srcNodes.length && (i+1)<destNodes.length ) {
int expected = srcNodes[i+1];
int actual = destNodes[i];

if(actual!=expected) return false;
}
}
return true;
}

public static boolean isTopologyCircular(int[] srcNodes, int[] destNodes) {
int first = srcNodes[0];
int last = destNodes[destNodes.length-1];
return first == last && isTopologyBus(srcNodes,destNodes);
}

public static boolean isTopologyStar(int[] srcNodes, int[] destNodes) {
for(int i=1; i<destNodes.length; i++) {
if(destNodes[0] != destNodes[i]) return false;
}
return true;
}
}``````

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

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

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

If it is sure that it should be one of the topologies(star, bus, circular)

``````if(no of nodes == no of connections)
CIRCULAR
else if(all connections point to same destination or the second array has only one unique element)
STAR
else
BUS``````

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.