HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Computing the relative magnitude of the values in one double array to another

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thearrayonedoubleanothervaluesmagnituderelativecomputing

Problem

An interview question I received:


You are given two unsorted lists of positive doubles



  • x contains the results from experiment 1



  • y which contains the results from experiment 2.





All results from the second experiment y are improvements over x
by i percent, where i is an integer. That is to say that given a
result from x, the corresponding result from y will be an i
percent improvement.


However, the lists are not parallel; that is to say x[0] does not
necessarily correspond to y[0]. Find the value of i.


Note: must be written in Java 7


Some examples


Inputs:

y = [1.0]
x = [1.0]




Output:

0




Inputs:

y = [2.2999999999999998, 15.0, 102.40000000000001, 3486.8000000000002]
x = [23.0, 150.0, 1024.0, 34868.0]




Output:

90




Inputs:

y = [23, 11.1, 50.4]
x = [22.2, 46, 100.8]




Output:

50




Note in the last case where 46 -> 23 and 22.2 -> 11.1 but the
index of 46 is not the index of 23, etc.

My Solution

import java.util.Arrays;

//Time complexity: O(n)
//Space complexity: O(1)
public class Solution{   
    public static int solution(double[] secondRun, double[] firstRun) {

        Double firstRunMax = findMax(firstRun),
               secondRunMax = findMax(secondRun);

        return (int)((1.0-secondRunMax/firstRunMax)*100);
    }

    private static double findMax(double[] array){
        double max = -Double.MAX_VALUE;

        for(double d : array){
            if(d > max){
                max = d;
            }
        }

        return max;
    }
}


Strengths: Very simple, as efficient as possible (I think?), ignores most of the data

Weakness: the return line is a little convoluted. Perhaps there is a better way to simplify it also. Also I thought I would be able to find a simple Arrays.Max method or something in Java, but I didnt see any during a quick glance.

Solution

You are auto-boxing the results of your function call, only to un-box them later. If everything else is using double (a primitive), there is no reason to declare the local variables in solution() as Double (an object). This doesn't effect the big-O of your solution, but it is extra operations for the JVM to execute.

Give your class and public method better names. If you actually needed this in production code, there would be no way to know what this code did. The names tell you nothing and there is no documentation.

There is a max(), but it works on Collections, not arrays. However, writing your own max method is not that hard, so it is up to you if you want to replace it.

Note: Until seeing you question, I didn't know that Double.MIN_VALUE and Integer.MIN_VALUE do not represent the same concept for the respective type. This isn't your fault, but it is unintuitive. Explination

Context

StackExchange Code Review Q#77043, answer score: 4

Revisions (0)

No revisions yet.