注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
五轮。
第一轮coding。白人小哥。完了想不起面哪道题啊。想起来再发。小哥人还好,题也不难印象中。
第二轮coding。像是西班牙小哥,还有一个sha
一个星期后recruiter发邮件说挂了。说system design 不行。- class Solution {
- public static void main(String[] args) {
- Evaluator evaluator1 = new Evaluator(2, 0.05);
- // Data array: [210.0, 185.0, 200.0]
- // Read in 210.0, average is 210.0
- // Read in 185.0, average is 210.0 - dip found
- // Read in 200.0, average is 205.0
- System.out.println(evaluator1.ingestData(210.0));
- System.out.println(evaluator1.ingestData(185.0));
- System.out.println(evaluator1.ingestData(200.0));
- //2nd test case
- // Data array: [505.2, 454.68, 553.32, 892.7, 499.76, 88.23, 477.1]
- // Read in 505.2, average is 505.2.
- // Read in 454.68, average is 479.94 =>
- // Read in 553.32, average is 479.94 - spike found.
- // Read in 892.7, average is 479.94 - spike found.
- // Read in 499.76, average is 477.22 =>
- // Read in 88.23, average is 477.22 - dip found.
- // Read in 477.1, average is 488.43
- Evaluator evaluator2 = new Evaluator(2, 0.05);
- double[] input2 = new double[]{505.2, 454.68, 553.32, 892.7, 499.76, 88.23, 477.1};
- System.out.println("========Test Case 2======");
- for (double num: input2) {
- System.out.println(evaluator2.ingestData(num));
- }
- }
- // private static void useCase1() {
- // // 1st test case
- // // Sliding window size: 2
- // // Data array: [210.0, 185.0, 200.0]
- // // Read in 210.0, average is 210.0
- // // Read in 185.0, average is 197.5
- // // Read in 200.0, average is 192.5
- // // Evaluator evaluator = new Evaluator(2);
- // // System.out.println(evaluator.ingestData(210.0));
- // // System.out.println(evaluator.ingestData(185.0));
- // // System.out.println(evaluator.ingestData(200.0));
- // // 2nd test case
- // // Sliding window size: 2
- // // Data array: [505.2, 454.68, 553.32, 892.7, 499.76, 88.23, 477.1]
- // // Read in 505.2, average is 505.2
- // // Read in 454.68, average is 479.94
- // // Read in 553.32, average is 504.0
- // // Read in 892.7, average is 723.01
- // // Read in 499.76, average is 696.23
- // // Read in 88.23, average is 294.0
- // // Read in 477.1, average is 282.67
- // Evaluator evaluator2 = new Evaluator(2);
- // double[] input2 = new double[]{505.2, 454.68, 553.32, 892.7, 499.76, 88.23, 477.1};
- // System.out.println("========Test Case 2======");
- // for (double num: input2) {
- // System.out.println(evaluator2.ingestData(num));
- // }
- // // last test case
- // // Sliding window size: 5
- // // Data array: [780.0, 331.0, 412.0, 403.0, 978.0, 372.0, 306.0, 355.0, 698.0, 487.0]
- // // Read in 780.0, average is 780.0
- // // Read in 331.0, average is 555.5
- // // Read in 412.0, average is 507.67
- // // Read in 403.0, average is 481.5
- // // Read in 978.0, average is 580.8
- // // Read in 372.0, average is 499.2
- // // Read in 306.0, average is 494.2
- // // Read in 355.0, average is 482.8
- // // Read in 698.0, average is 541.8
- // // Read in 487.0, average is 443.6
- // System.out.println("========Test Case 3======");
- // Evaluator evaluator3 = new Evaluator(5);
- // double[] input3 = new double[]{780.0, 331.0, 412.0, 403.0, 978.0, 372.0, 306.0, 355.0, 698.0, 487.0};
- // for (double num: input3) {
- // System.out.println(evaluator3.ingestData(num));
- // }
- // }
- }
- class Evaluator {
- int k;
- Queue<Double> queue;
- double currSum;
- double currAverage;
- double threshold;
- public Evaluator(int windowSize, double threshold) {
- this.k = windowSize;
- this.threshold = threshold;
- this.queue = new LinkedList<>();
- this.currSum = 0;
- this.currAverage = 0;
- }
- public double ingestData(double next) {
- // currAverage => threahold [a, b];
- // potentail incoming average => newAverage;
- // is this a dip or a spike
- // if yes, ignore it
- // if not, continue ingesting the data
- // first data point is always accepted
- if (queue.isEmpty()) {
- queue.add(next);
- currSum += next;
- double val = currSum / queue.size();
- currAverage = (double) Math.round(val * 100) / 100; // round to 2 decimal point
- return currAverage;
- }
- double[] thresholds = new double[] {currAverage * ( 1 - threshold), currAverage * (1 + threshold)};
- // incoming average
- // curretn data poitns + incoming datapoint <= k
- if ((queue.size() + 1) <= k) {
- double newAverage = (currSum + next) / (queue.size() + 1);
- if (newAverage < thresholds[0]) {
- // ignore this data point. but also indicate the dip found
- System.out.println("Dip found");
- return currAverage;
- }
- if (newAverage > thresholds[1]) {
- // ignore this data point. but also indicate the dip found
- System.out.println("Spike found");
- return currAverage;
- }
- queue.add(next);
- currSum += next;
- double val = currSum / queue.size();
- currAverage = (double) Math.round(val * 100) / 100; // round to 2 decimal point
- return currAverage;
- }
- // queue is going to be overflow with new data point
- double newAverage = (currSum + next - queue.peek()) / k;
- if (newAverage < thresholds[0]) {
- // ignore this data point. but also indicate the dip found
- System.out.println("Dip found");
- return currAverage;
- }
- if (newAverage > thresholds[1]) {
- // ignore this data point. but also indicate the dip found
- System.out.println("Spike found");
- return currAverage;
- }
- System.out.println("incoming " + next + " currSum " + ((currSum + next - queue.peek()) /2 ) + " currAverage: " + currAverage + " newAverage " + newAverage + " queue size " + queue.size());
- System.out.println(thresholds[0] + " " + thresholds[1]);
- queue.add(next);
- currSum += next - queue.remove();
- currAverage = (double) Math.round(newAverage * 100) / 100; // round to 2 decimal point
- return currAverage;
- // curr data points + incoming data point > k
- // // return the moving average for the last k data points;
- // queue.add(next);
- // currSum += next;
- // if (queue.size() > k) {
- // currSum -= queue.remove();
- // }
- // double val = currSum / queue.size();
- // currAverage = (double) Math.round(val * 100) / 100; // round to 2 decimal point
- // return currAverage;
- }
- }
复制代码 |