Google Phone interview -- Bay area
Anonymous User
447

Interviewer basically wanted the count of all reciprocated edges and way to store these edges.

He pasted the below code snippets and wanted me implemented these methods. I gave a solution which involved storing the reciprocated edges in a map. But go to know I was rejected :-(


// Get an object representing the edge

// from the node with ID "a"

// to the node with ID "b"

// returns null if no edge exists

//List...

Edge e = graph.getEdge("a", "b");

// arbitrary data stored on the edge

EdgeData eData = e.getData();

// IDs of source and destination nodes

String sourceId = e.getSourceId();

String destinationId = e.getDestinationId();

for (Edge edge : graph.getAllEdges()) {

// executes once per edge of the graph in arbitrary order

}

/**

  • Returns the number of reciprocated edges in the given directed graph.

  • A reciprocated edge is a pair of nodes (X, Y) such that there is an edge

  • from X to Y and an edge from Y to X.

*/

int <NODE_DATA, EDGE_DATA> countReciprocatedEdges(

DirectedGraph<NODE_DATA, EDGE_DATA> graph) {

// implementation here

}

return countReciprocatedEdges;

a -> b

b -> a

b -> c

c -> a

// create an empty graph

DirectedGraph<NodeData, EdgeData> g = new DirectedGraph<>();

// Create nodes named "a", and "b", each with associated data

g.addNode("a", aNodeData);

g.addNode("b", bNodeData);

// return node with given name or null if none exists

Node nodeA = g.getNode("a");

// Create an edge from "a" to "b" with arbitrary associated data

g.addEdge("a", "b", abEdgeData);

class Node<NODE_DATA> {

private NODE_DATA data;

private String id;

Node<NODE_DATA>(String id, NODE_DATA data) {

this.data = data;

this.id = id;

}

public NODE_DATA getData() {

return data;

}

public String getId() {

return id;

}

}

class Edge<EDGE_DATA> {

Edge(String sourceId, String dstId, EDGE_DATA edgeData) {

this.sourceId = sourceId;

this.dstId = dstId;

this.edgeData = edgeData;

}

}

Edge e = new Edge("a", "b", "edgeData");

class DirectedGraph<NODE_DATA, EDGE_DATA> {

//List<Edge<EDGE_DATA>> edges = new ArrayList<Edge<EDGE_DATA>>();

public void addEdge(String source, String destination, EDGE_DATA edgeData){

}

public Edge getEdge(String source, dest){

}

public List getAllEdges(){

}

Comments (1)