t*******7 发帖数: 63 | 1 刚面完,上来给大家SHARE下。
晚打来5分钟, 上来先问RESEARCH PROJECT 暖场。
然后问一题:
给一个SEQUENCE, 给一个 window size, 求 running average.
constructor里给WINDOW SIZE, 实现一个 next(double input) interface 返回
the running average till input:
For example:
window size = 5;
input: {1,2,3,4} output: 2.5
input: {1,2,3} output: 2
input: {1,2,3,4,5} output: 3
input: {1,2,3,4,5,6} output: 4
感觉面得一般,面试官有些口音,有时听不太清。
贴上我的代码:
public class SequencePreprocessor {
private LinkedList newList = null;
// get the window size
private int winSize = 0;
private double sum = 0;
public SequencePreprocessor(int winSize) throws Exception {
if (winSize<=0) {
throw new Exception("The input window size illegal");
}
this.newList = new LinkedList();
this.winSize = winSize;
}
//get the next
public double next(double input) {
if (newList.size() < this.winSize) {
sum += input;
newList.add(input);
return this.sum/newList.size();
}else {
double output = newList.remove();
newList.add(input);
this.sum = this.sum - output + input;
return this.sum/this.winSize;
}
}
}
| y***n 发帖数: 1594 | |
|