Skip to content

Commit 29e3537

Browse files
InterviewQuestions/src/package1/SetValueInDependencyGraph.java
1 parent cea1eac commit 29e3537

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package package1;
2+
3+
import java.util.*;;
4+
5+
6+
public class SetValueInDependencyGraph {
7+
public static void main(String...strings){
8+
9+
Map<String, List<String>> dependenceMap = new HashMap();
10+
11+
List<String> list1 = new ArrayList();
12+
list1.add("B");
13+
list1.add("C");
14+
15+
List<String> list2 = new ArrayList();
16+
list2.add("A");
17+
list2.add("C");
18+
list2.add("D");
19+
20+
List<String> list3 = new ArrayList();
21+
list3.add("E");
22+
23+
List<String> list4 = new ArrayList();
24+
list4.add("A");
25+
26+
dependenceMap.put("A", list1);
27+
dependenceMap.put("B", list2);
28+
dependenceMap.put("C", list3);
29+
dependenceMap.put("E", list4);
30+
31+
SetValueInDependencyGraph.Method.setValue_v2("A", 1, dependenceMap);
32+
33+
}
34+
35+
static class Method{
36+
public static void setValue_v2(String columnName, int value, Map<String, List<String>> depedenceMap){
37+
Map<String, Integer> result = new HashMap();
38+
result.put(columnName, value);
39+
40+
Queue<String> queue = new LinkedList();
41+
queue.offer(columnName);
42+
43+
while(!queue.isEmpty()){
44+
int size = queue.size();
45+
for(int i = 0; i < size; i++){
46+
String currCol = queue.poll();
47+
int thisValue = result.get(currCol);
48+
if(!depedenceMap.containsKey(currCol)) continue;
49+
for(String dep : depedenceMap.get(currCol)){
50+
if(!result.containsKey(dep)){
51+
result.put(dep, thisValue+1);
52+
queue.offer(dep);
53+
}
54+
}
55+
}
56+
}
57+
58+
for(String key : result.keySet()){
59+
System.out.println(key + "\t" + result.get(key));
60+
}
61+
}
62+
63+
64+
public void setValue_v1_stopped_by_interviewer(String columnName, int value, Map<String, List<String>> depedenceMap){
65+
Map<String, Integer> result = new HashMap();
66+
for(String colName : depedenceMap.keySet()){
67+
if(!result.containsKey(colName)){
68+
result.put(colName, value);
69+
}
70+
for(String dep : depedenceMap.get(colName)){
71+
if(!result.containsKey(dep)){
72+
result.put(dep, result.get(colName)+1);
73+
}
74+
}
75+
}
76+
}
77+
78+
79+
}
80+
}

0 commit comments

Comments
 (0)