ADP Interview Question
Java DevelopersCountry: United States
The 2 earlier responses from are incorrect (just adding a set of children and detecting if they exist). Example:
a-b
b-c
d-a
There is no cycle but the code suggested using a set of children will return true.
The answer is a simple DFS/BFS marking nodes as visited as you traverse. If you find a node that was already visited, there is a cycle.
Create a list out of tuples, check list for curcular by having 2 pointers slow and fast move them.
public class DetectCycle {
/**
* @param args
*/
public static void main(String[] args) {
Tuple tuple1 = new Tuple('a','b');
Tuple tuple2 = new Tuple('b','c');
Tuple tuple3= new Tuple('c','b');
List<Tuple> tuplesList = new ArrayList<Tuple>();
tuplesList.add(tuple2);
tuplesList.add(tuple1);
tuplesList.add(tuple3);
Collections.sort(tuplesList);
for(int i=0; i<tuplesList.size()-1; i++) {
if(tuplesList.get(i).getChild() == tuplesList.get(i+1).getParent()) {
for(int j=i+1;j>=0;j--) {
if(tuplesList.get(i+1).getChild() == tuplesList.get(j).getParent()) {
System.out.println("Cycle detected");
}
}
}
}
}
}
class Tuple implements Comparable<Tuple>{
char parent;
char child;
Tuple(char parent,char child) {
this.parent = parent;
this.child = child;
}
public char getParent() {
return parent;
}
public void setParent(char parent) {
this.parent = parent;
}
public char getChild() {
return child;
}
public void setChild(char child) {
this.child = child;
}
@Override
public int compareTo(Tuple tuple) {
if(this.getParent() < tuple.getParent()) {
return -1;
}
else if(this.getParent() > tuple.getParent()) {
return 1;
}
else {
return 0;
}
}
}
Simple solution using a symbol table and a digraph written in Java.
import java.util.*;
public class FindCycle {
public static void main(String... args) {
Map<String, String> map = new HashMap<>();
map.put("a", "b");
map.put("b", "c");
map.put("c", "a");
System.out.println(hasCycle(map));
}
private static boolean hasCycle(Map<String, String> map) {
// Create symbol table
SymbolTable symtab = new SymbolTable();
for (String key : map.keySet()) {
symtab.add(key);
symtab.add(map.get(key));
}
// Create graph
Digraph digraph = new Digraph(symtab.size());
for (String key : map.keySet()) {
int v = symtab.get(key);
int w = symtab.get(map.get(key));
digraph.addEdge(v, w);
}
// Check cycle
boolean visited[] = new boolean[digraph.V()];
for (int v = 0; v < digraph.V(); v++) {
visited[v] = true;
for (int w : digraph.getAdjacent(v)) {
if (visited[w])
return true;
visited[w] = true;
}
}
return false;
}
/**
* Simple symbol table.
*/
private static class SymbolTable {
private final Map<String, Integer> table;
private int seq;
public SymbolTable() {
this.table = new HashMap<>();
}
public void add(String symbol) {
if (table.get(symbol) == null)
table.put(symbol, seq++);
}
public int get(String symbol) {
return table.get(symbol);
}
public int size() {
return table.size();
}
}
/**
* Simple directed graph.
*/
private static class Digraph {
private final int V;
private int E;
private ArrayList<Integer>[] adj;
public Digraph(int V) {
this.V = V;
adj = (ArrayList<Integer>[])new ArrayList[V];
for (int i = 0; i < V; i++) {
adj[i] = new ArrayList<>();
}
}
public void addEdge(int v, int w) {
adj[v].add(w);
++E;
}
public Iterable<Integer> getAdjacent(int v) {
return adj[v];
}
public int V() {
return V;
}
}
}
- glebstepanov1992 May 25, 2014