|
| 1 | +package iurii.job.interview.codility.flow_traders; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.math.BigDecimal; |
| 6 | +import java.math.RoundingMode; |
| 7 | +import java.text.DecimalFormat; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 11 | +import java.util.concurrent.LinkedBlockingDeque; |
| 12 | +import java.util.concurrent.atomic.AtomicLong; |
| 13 | +import java.util.concurrent.atomic.AtomicLongArray; |
| 14 | + |
| 15 | +/** |
| 16 | + * Complete class function to supply new price and return average of all the prices (with supplied one) |
| 17 | + * 1) implement the method (we can not store all the prices in the memory) |
| 18 | + * 2) implement method to calculate average only for last "precision" prices |
| 19 | + * 3) make code thread safe |
| 20 | + */ |
| 21 | +public class AverageCalculatorCodePairInterviewTest { |
| 22 | + |
| 23 | + // one of the issues is rounding |
| 24 | + // here is the solution for rounding with DecimalFormat |
| 25 | + private final DecimalFormat df = new DecimalFormat("#.###"); |
| 26 | + |
| 27 | + { |
| 28 | + df.setRoundingMode(RoundingMode.HALF_UP); |
| 29 | + String stringValue = df.format(1.23451); |
| 30 | + double roundedValue = Double.parseDouble(stringValue); |
| 31 | + System.out.println(stringValue); |
| 32 | + System.out.println(roundedValue); |
| 33 | + // or |
| 34 | + double rounded3Digits = Math.round(1.23451 * 1000) / 1000; |
| 35 | + System.out.println(rounded3Digits); |
| 36 | + // or |
| 37 | + System.out.println(new BigDecimal(String.valueOf(1.2347)).setScale(3, BigDecimal.ROUND_HALF_UP)); |
| 38 | + } |
| 39 | + |
| 40 | + // last average value |
| 41 | + private double average = 0; |
| 42 | + // count |
| 43 | + private long count = 0; |
| 44 | + |
| 45 | + // average with new value |
| 46 | + public double supplyNextPrice(long price) { |
| 47 | + average = (average * count + price) / (++count); |
| 48 | + return average; |
| 49 | + } |
| 50 | + |
| 51 | + // for precision |
| 52 | + // number of last prices |
| 53 | + private final int precision = 20; |
| 54 | + private final List<Long> lastPrices = new ArrayList<>(precision); |
| 55 | + private int index = 0; |
| 56 | + |
| 57 | + public double supplyNextPriceWithPrecision(long price) { |
| 58 | + lastPrices.set(index++ % precision, price); |
| 59 | + return lastPrices.parallelStream().mapToLong(i -> i).average().orElse(0.0); |
| 60 | + } |
| 61 | + |
| 62 | + private final Object lock = new Object(); |
| 63 | + |
| 64 | + // ! synchronized on private final object is better than synchronized on the instance of class, |
| 65 | + // cause someone can synchronize on this object as well |
| 66 | + public double supplyNextPriceWithPrecisionThreadSafe1(long price) { |
| 67 | + synchronized (lock) { |
| 68 | + lastPrices.set(index++ % precision, price); |
| 69 | + return lastPrices.parallelStream().mapToLong(i -> i).average().orElse(0.0); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private CopyOnWriteArrayList copyOnWriteArrayList = new CopyOnWriteArrayList(); |
| 74 | + private AtomicLongArray atomicLongArray = new AtomicLongArray(20); |
| 75 | + private LinkedBlockingDeque<Long> deque = new LinkedBlockingDeque<>(20); |
| 76 | + |
| 77 | + public double supplyNextPriceWithPrecisionThreadSafe2(long price) { |
| 78 | + return 0; |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void test() { |
| 83 | + AverageCalculatorCodePairInterviewTest test = new AverageCalculatorCodePairInterviewTest(); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments