# 两道面试题解答之二 Moving Average, Moving Median

Question 1 / 2 (Moving Average)

At Bridgewater we have a philosophy of systemization - we build automated trading platforms where we can rapidly iterate and improve our strategies.  As such, data quality becomes important to avoid costly errors. In order to ensure we're using accurate data for trading decisions, we've also automated much of the data validation logic. While some error checking (e.g. detecting 0s in data which should only contain positive numbers) is easy, other error checking is much harder (e.g. sporadic aberrations in pricing data). Most error checking done is through stream algorithms as the quantity and pace of data is far too much to analyze the data statically.

Write a moving average function that will take the average of the trailing N samples of market data samples. The first line of the input will be an integer, N, which will define the window of the moving average function. Output the moving average of the data starting with then Nth data point and continuing until the end of the file. You should use 3 decimal places of precision.

Note: Your algorithm should process the information continually as a stream i.e. begin outputting the moving average after the Nth datapoint and continuing until the end of the input file.

For instance, the following input:

2
1.000
2.000
3.000
4.000

Should yield the output:

1.500
2.500
3.500

package bridgewater;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MovingAverageSolution {

private List<String> inputs = new ArrayList<String>();
private int winSize = 0;

public MovingAverageSolution(){
}

public static void main(String[] args) throws Exception {
BufferedReader br = null;
MovingAverageSolution solution = new MovingAverageSolution();
try {
InputStream inputStream = new FileInputStream("input1.txt");

String input = br.readLine();
while(input != null){
solution.process(solution.inputs);
}

} finally {
if (br != null)
br.close();
}
}

public void process(List<String> inputs) {
//		System.out.println(inputs.size());
if(winSize<=0 || inputs.size() < winSize){
return;
}

double sum = 0;
for(int i=inputs.size()-1; i>=inputs.size()-winSize; i--){
sum += Double.parseDouble(inputs.get(i));
}
double average = sum*1.0 / winSize;
System.out.println(String.format("%.3f", average));
}

}

Question 2 / 2 (Moving Median and Error Detection)

Write a moving median function that will take the median of the trailing N samples of market data samples and identify deviations from that median that vary by more than a percentage, P. The first line of the input will be an integer, N, which will define the window size of the moving median function; the second line will be a decimal, P, which defined the maximumpercentage change allowed in the data. Output any number which exceeds this percentage change (note: you should still include this numbers in your moving median). You should use 3 decimal places of precision.

Note: Your algorithm should process the information continually as a stream i.e. begin outputting the moving average after the Nth datapoint and continuing until the end of the input file.

For instance, the following input:

3
0.500
5.000
5.000
7.000
10.000
7.000
7.000
12.000
7.000
7.000

Should yield the output:

10.000
12.000

Since 10 and 12 each vary by more than 50% from the median of the previous 3 values.

package bridgewater;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MovingMedianSolution {

private List<String> inputs = new ArrayList<String>();
private int winSize = 0;
private double maxChange = 0;

public MovingMedianSolution(){
}

public static void main(String[] args) throws Exception {
BufferedReader br = null;
MovingMedianSolution solution = new MovingMedianSolution();
try {
InputStream inputStream = new FileInputStream("input2.txt");

String input = br.readLine();
while(input != null){
solution.process(solution.inputs);
}

} finally {
if (br != null)
br.close();
}
}

public void process(List<String> inputs) {
if(winSize<=0 || maxChange<0 || inputs.size() < winSize+1){
return;
}

ArrayList<Double> al = new ArrayList<Double>();
for(int i=inputs.size()-2; i>=inputs.size()-1-winSize; i--){
}
Collections.sort(al);

double median = al.get(al.size()/2);		// even size
if((al.size() & 1) == 0){			// When it is odd size
median = al.get(al.size()/2-1);
}

double cur = Double.parseDouble(inputs.get(inputs.size()-1));
if(Math.abs(cur-median)*1.0/median > maxChange){
System.out.println(String.format("%.3f", cur));
}
}

}

• 本文已收录于以下专栏：

举报原因： 您举报文章：两道面试题解答之二 Moving Average, Moving Median 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)