### Question:
PercentileBuckets { public static void percentiles(long[] counts, double[] pcts, double[] results) { Preconditions.checkArg(counts.length == BUCKET_VALUES.length, "counts is not the same size as buckets array"); Preconditions.checkArg(pcts.length > 0, "pct array cannot be empty"); Preconditions.checkArg(pcts.length == results.length, "pcts is not the same size as results array"); long total = 0L; for (long c : counts) { total += c; } int pctIdx = 0; long prev = 0; double prevP = 0.0; long prevB = 0; for (int i = 0; i < BUCKET_VALUES.length; ++i) { long next = prev + counts[i]; double nextP = 100.0 * next / total; long nextB = BUCKET_VALUES[i]; while (pctIdx < pcts.length && nextP >= pcts[pctIdx]) { double f = (pcts[pctIdx] - prevP) / (nextP - prevP); results[pctIdx] = f * (nextB - prevB) + prevB; ++pctIdx; } if (pctIdx >= pcts.length) break; prev = next; prevP = nextP; prevB = nextB; } double nextP = 100.0; long nextB = Long.MAX_VALUE; while (pctIdx < pcts.length) { double f = (pcts[pctIdx] - prevP) / (nextP - prevP); results[pctIdx] = f * (nextB - prevB) + prevB; ++pctIdx; } } private  PercentileBuckets(); static long[] asArray(); static T[] map(Class<T> c, Function<Long, T> f); static long get(int i); static int length(); static int indexOf(long v); static long bucket(long v); static void percentiles(long[] counts, double[] pcts, double[] results); static double percentile(long[] counts, double p);  }### Answer:
@Test public void percentiles() { long[] counts = new long[PercentileBuckets.length()]; for (int i = 0; i < 100_000; ++i) { ++counts[PercentileBuckets.indexOf(i)]; } double[] pcts = new double[] {0.0, 25.0, 50.0, 75.0, 90.0, 95.0, 98.0, 99.0, 99.5, 100.0}; double[] results = new double[pcts.length]; PercentileBuckets.percentiles(counts, pcts, results); double[] expected = new double[] {0.0, 25e3, 50e3, 75e3, 90e3, 95e3, 98e3, 99e3, 99.5e3, 100e3}; double threshold = 0.1 * 100_000; Assertions.assertArrayEquals(expected, results, threshold); for (int i = 0 ; i < results.length; ++i) { threshold = 0.1 * expected[i] + 1e-12; Assertions.assertEquals(expected[i], results[i], threshold); } }