Audible OA

1 Like

谢谢分享!

Given a set of CSV transactions, generate 2 files.

Product, average quantity per order
Most common brand per product

public static void generateFiles(String input_file) {
    // Write your code here
    try{
        Path path = Paths.get(input_file);
        byte[] bytes = Files.readAllBytes(path);
        List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
        // System.out.println(lines);

        HashMap<String, Integer> counts = new HashMap();
        Map<String, Map<String, Integer>> brands = new HashMap<>();
        for(String line: lines){
            String words[] = line.split(",");
            counts.put(words[2], counts.getOrDefault(words[2], 0)+Integer.parseInt(words[3]));

            Map<String, Integer> br = brands.get(words[2]);
            if(br==null) br = new HashMap<>();
            br.put(words[4], br.getOrDefault(words[4], 0 ) + 1);
            brands.put(words[2], br);
        }
        // System.out.println(counts);
        // System.out.println(brands);
        String output_file1 = "0_" + input_file;
        String output_file2 = "1_" + input_file;

        StringBuilder answer = new StringBuilder();
        for(String key: counts.keySet()){
            answer.append(key);
            answer.append(",");
            answer.append(1.0*counts.get(key)/lines.size());
            answer.append("\n");
        }
        StringBuilder answer2 = new StringBuilder();
        for(String key: brands.keySet()){
            Map<String, Integer> br = brands.get(key);
            int max = -1;
            String maxBrand = "";
            for(String b: br.keySet()){
                if(br.get(b)> max){
                    max = br.get(b);
                    maxBrand = b;
                }
            }
            answer2.append(key);
            answer2.append(",");
            answer2.append(maxBrand);
            answer2.append("\n");
        }
        // System.out.println(answer2.toString());
        Files.write(Paths.get(output_file1), answer.toString().trim().getBytes());
        Files.write(Paths.get(output_file2), answer2.toString().trim().getBytes());
        return;
    }catch(Exception e){}
    return;
    // HashMap<String, HashMap<String,Integer>> hm2 = new HashMap();

    }

Audible OA

hackerrank两道题, ghc 发来的

第一道 shopping data, 给一个csv, reutrn 两个csv。shopping data 数据差不多像这样,

order id, location, shoes, 3, Air

order id, location, shoes, 3, Nike

order id, location, knif, 3, some brand

第一个结果求不同商品的平均order

shoes,3.78

hats, 4

第二个结果求most popular 商品

shoe s, Air

knif, some brand

第二道 team formation,
就是给一个score 的list 比如 [1,2,37,8,9,13,6],然后给一个m,再给一个team number代表要找的score 的数量

如果 m=2, Team number =2

首先找到这个list 前2个和最后2个,[1,2]和 [13,6], 这四个数里面13 最大,选出13

list 变成 [1,2,37,8,9,6]

再找前2个和最后2个, [1,2]和[9,6], 其中9最大

return 13+9=22

考了 shopping data 和 team forma tion两道题 很简单