Why does my function return an incorrect answer for the average salary question

Viewed 163

I am pretty new to javascript and working on problems from leetcode.

The description included is: "Given an array of unique integers salary where salary[i] is the salary of the employee i.

Return the average salary of employees excluding the minimum and maximum salary."

When I run my code it says that I have the incorrect output when the following array is passed in.

[25000,48000,57000,86000,33000,10000,42000,3000,54000,29000,79000,40000]
Expected Output: 41700.00000
My Output: 41000.00000

I've compared my code to other submissions and as far as I can tell mine should run the same. Here is my code:

function average(salary) {
  var sortedSalary = salary.sort();
  var total = sortedSalary.reduce((curr, acc) => {
    return curr + acc
  }, 0);
  var result = (total - sortedSalary[0] - sortedSalary[sortedSalary.length - 1]) / (sortedSalary.length - 2);
  return result;
};
console.log(average([25000,48000,57000,86000,33000,10000,42000,3000,54000,29000,79000,40000]));

Thank you for any insight into this.

3 Answers

You're sorting the array alphabetically, not by the smallest number, so you end up removing the wrong numbers.

Use sort((a, b) => a - b) instead:

const salary = [25000,48000,57000,86000,33000,10000,42000,3000,54000,29000,79000,40000]

function average(salary) {
  var sortedSalary = salary.sort((a, b) => a - b);
  var total = sortedSalary.reduce((curr, acc) => { return curr + acc }, 0);
  var result = (total - sortedSalary[0] - sortedSalary[sortedSalary.length - 1]) / (sortedSalary.length - 2);
  return result;
};

console.log(average(salary))

const average = salary => salary
  .sort((a,b) => a - b) // sort numerically
  .filter((_,i,l) => i > 0 && i < l.length - 1) // remove first and last index
  .reduce((a,s) => a + s) / (salary.length - 2) // calculate average

console.log(average([25000,48000,57000,86000,33000,10000,42000,3000,54000,29000,79000,40000]))

It does not need to be sorted at all. Apart from that, sorting is O(N Log N) not sufficiently efficient here. You just have to come up with an O(N) algorithm for this problem.

This'll pass:

var average = function(salary) {
    if (salary.length < 3) {
        return 0
    }

    let min = salary[0]
    let max = salary[0]
    let sum = 0

    for (let sal of salary) {
        if (sal > max) {
            max = sal
        }

        if (sal < min) {
            min = sal
        }

        sum += sal
    }

    return (sum - max - min) / (salary.length - 2)
};

References

  • For additional details, you can see the Discussion Board. There are plenty of accepted solutions with a variety of languages and explanations, efficient algorithms, as well as asymptotic time/space complexity analysis1, 2 in there.

For interviews:

  • We'd like to write bug-free and clean codes based on standards and conventions (e.g., 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1).

  • For easy questions, brute force algorithms usually get accepted. For interviews, brute force is less desired, especially if the question would be an easy level, like the one you're solving.

Related