public class Solution1 {//assume the doc path is unique, and we can use it as ID
HashMap<String, HashMap<String, Integer>> wordPathHm;//<word, <docID/path, number of instances>>
public Solution1(){
wordPathHm = new HashMap<String, HashMap<String, Integer>>();
}
public void buildIndex(String path) throws IOException{
Scanner doc = new Scanner(new FileReader(path));
while(doc.hasNextLine()){
String line = doc.nextLine();
String[] words = line.split("\\s+");
for(String word : words){
if(wordPathHm.containsKey(word)){//if the word already exists, update the hashmap
HashMap<String, Integer> oldlist = wordPathHm.get(word);
Integer currCnt = oldlist.get(path);
oldlist.put(path, currCnt+1);
} else {//if the word does not exist, add a new entry
HashMap<String, Integer> newlist = new HashMap<String, Integer>();
newlist.put(path, 1);
wordPathHm.put(word, newlist);
}
}
}
doc.close();
}
public int getDocInstanceNum(String word, String docPath){
if(wordPathHm.containsKey(word)){
HashMap<String, Integer> hmRecord = wordPathHm.get(word);
if(hmRecord.containsKey(docPath)){
return hmRecord.get(docPath);
} else {
return -2; //the doc does not exist
}
} else {
return -1; // the word does not exist
}
}
}
然后又问如果用户很多该怎么办
DHT
hash(docID/path) -> which server should be queried for this doc
the queried server should return the path of the documents, and how many instances in each document
multiple hashmaps
each hashmap return the number of instances for this key word, and add the count together
2. 问了操作系统调度问题,我刚好看了一点
Completely Fair Scheduler Linux 2.6
we have different priority weights for different tasks
it is difficult for us to predict how much time
build red-black tree -> an approximately balanced binary search tree
if vrt is smaller , it means it should be assigned to the processor earlier
start from leftmost nodes
18
12 26
4 16 20 28
又问top K words
package cisco;
import java.io.FileReader;
import java.util.*;
public class Solution2 {
public List<String> getTopKWords(String path, int k) throws Exception{
List<String> result = new ArrayList<String>();
if(k <= 0)
return result;