Uber Codesignal Phonescreen - Top Sort Question!
Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2021-08-04Added By Anonymous
Source: Link
Company: Uber
Interview Stage: Phone Screen
Original Post Content:
Question:
1Given an input of a list of lists, each list contains a list of node_ids. The order of the list [A,B] represents a one way
2connection from node A to node B, and node_ids that are the same between different lists are the same node.
3We want to find the number of nodes in the longest chain across all the input lists.
4
5Lets say we are given list -> ["A", "C", "D"], then A connected to C, and C connected to D. This
6way we will be given list of lists which has several node links, we need to longest connected chain in this.
7
8Input:
9[["A", "C", "D"],
10 ["A", "B", "E"],
11 ["D", "F", "G", "H"],
12 ["Z", "X", "C", "Q", "R"]]
13
14Solution for longest chain - z, x, c, d, f, g, h
15Output: 7My Solution, hope this helps someone. This is straight forward Topological Sort Question I think, please feel free to correct me or ask questions if you have doubts in my implementation. Thank you!
1class Solution {
2
3 public int findLongestChain(List<List<String>> inputLists){
4 // map parent -> list(children)..
5 // map child -> parents (This is to get zero indegree elements, child that does not have parents)..
6 Map<String, List<String>> parentChild = new HashMap<>();
7 Map<String, List<String>> childParent = new HashMap<>();
8
9 for(List<String> list: inputLists){
10 for(int i=0; i<list.size()-1; i++){
11 parentChild.computeIfAbsent(list.get(i), k-> new ArrayList<>()).add(list.get(i+1));
12 if(i==0){
13 childParent.computeIfAbsent(list.get(i), k-> new ArrayList<>());
14 }
15 else{
16 childParent.computeIfAbsent(list.get(i+1), k-> new ArrayList<>()).add(list.get(i));
17 }
18
19 }
20
21 }
22
23 int maxSize = Integer.MIN_VALUE;
24 for(Map.Entry<String, List<String>> mapItem: childParent.entrySet()){
25 if(mapItem.getValue().size()==0){
26 int size = getChildList(parentChild, mapItem.getKey());
27 if(size > maxSize)
28 maxSize = size;
29 }
30 }
31
32 return maxSize;
33 }
34
35 public int getChildList(Map<String, List<String>> map, String input){
36
37 Queue<String> queue = new LinkedList<>();
38 Queue<Integer> sizeQueue = new LinkedList<>();
39 queue.offer(input);
40 sizeQueue.offer(1);
41 int maxSize = Integer.MIN_VALUE;
42
43 while(!queue.isEmpty()){
44 String node = queue.remove();
45 int size = sizeQueue.remove();
46
47 List<String> childList = map.get(node);
48
49 if(childList!=null){
50 for(String item:childList){
51 queue.offer(item);
52 size = size+1;
53 sizeQueue.offer(size);
54
55 }
56 }
57 else{
58 if(size>maxSize)
59 maxSize = size;
60 }
61
62
63 }
64 return maxSize;
65 }
66
67 public static void main(String[] args) {
68 List<List<String>> inputLists = new ArrayList<>();
69 inputLists.add(Arrays.asList("A", "C", "D"));
70 inputLists.add(Arrays.asList("A", "B", "E"));
71 inputLists.add(Arrays.asList("D", "F", "G", "H"));
72 inputLists.add(Arrays.asList("Z", "X", "C", "Q", "R"));
73 System.out.print(new Solution().findLongestChain(inputLists));
74
75 }
76}