ADP Interview Question for Java Developers


Country: United States




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

public boolean findCycle(List<Tuple> tuples) {
	Map<Character,Character> map = new HashMap();

	for(Tuple t : tuples) {
		if(map.get(t.getChild() != null) {
			return false;
		} else {
			map.put(t,getParent());
		}

		return true;
}

- glebstepanov1992 May 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- Anonymous May 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The answer is still not complete
tseudo code should be:
nodes= {all nodes}
while( nodes is not empty )
{
randomly pick a node A in nodes.
construct Spanning tree from A and return false if loop is found.
remove all nodes in spanning tree from A.
}
return true

- oceanmaster.wang May 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a list out of tuples, check list for curcular by having 2 pointers slow and fast move them.

- Reply May 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This works for a linked list where each node has exactly 1 descendant, but this problem seems to suggest a node can have multiple descendants.

- Sunny May 29, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Bharat May 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Diego Giagio June 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean findCycle(List<Tuple> tuples) {
	Set<Character> parents = new HashSet<Character>();

	for(Tuple t : tuples) {
		parents.add(t.getParent());
		if(parents.contains(t.getChild())) {
			return true;
		}
	}
	return false;
}

- Anonymous June 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean findCycle(List<Tuple> tuples) {
	Set<Character> parents = new HashSet<Character>();

	for(Tuple t : tuples) {
		parents.add(t.getParent());
		if(parents.contains(t.getChild())) {
			return true;
		}
	}
	return false;
}

- goldbug1832 June 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

just add all numbers to set. if duplicate detected, means cycle

- vikas May 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it would be proper if all elements are repeated twice or in even nos..then it should be said Cycle

- panpakr May 27, 2014 | Flag


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More