Student Grades - JavaScript Solution with O(n) Time Complexity


Given the grades of a student as an array, determine if the student has passed the class.

A Student has passed the class if the average of grades is 5 or more.

The average is defined as (grades[0] + grades[1] + ... + grades[n - 1]) / n

Example 1:

Input: grades = [4, 7, 5, 9, 8, 2]
Output: true
Explanation:
average = (4 + 7 + 5 + 9 + 8 + 2) / 6 =
= 35 / 6 = 5.833333333... >= 5

Example 2:

Input: grades = [4, 7, 5, 3, 8, 2]
Output: false
Explanation:
average = (4 + 7 + 5 + 3 + 8 + 2) / 6 =
= 29 / 6 = 4.83333.. < 5

Understanding the Problem

The core challenge of this problem is to determine if the average of a list of grades meets a certain threshold. This is a common problem in educational software where determining pass/fail status based on grades is essential.

Potential pitfalls include not correctly calculating the sum or average, or misunderstanding the threshold condition.

Approach

To solve this problem, we can break it down into three main steps:

  1. Compute the sum of the grades array.
  2. Compute the average by dividing the sum by the length of the array.
  3. Check if the average is greater than or equal to 5.

Let's start with a naive approach and then optimize it.

Naive Approach

The naive approach involves iterating through the array to compute the sum, then dividing by the length to get the average, and finally checking if the average meets the threshold.

Optimized Approach

The optimized approach is essentially the same as the naive approach since the problem is straightforward and the naive approach is already optimal with O(n) time complexity.

Algorithm

Here is a step-by-step breakdown of the algorithm:

  1. Initialize a variable sum to 0.
  2. Iterate through each grade in the array and add it to sum.
  3. Compute the average by dividing sum by the length of the array.
  4. Return true if the average is greater than or equal to 5, otherwise return false.

Code Implementation

/**
 * Function to determine if a student has passed based on their grades.
 * @param {number[]} grades - Array of grades.
 * @return {boolean} - True if the average grade is >= 5, otherwise false.
 */
function hasPassed(grades) {
  // Step 1: Compute the sum of the grades
  let sum = 0;
  for (let grade of grades) {
    sum += grade;
  }

  // Step 2: Compute the average
  const average = sum / grades.length;

  // Step 3: Check if the average is >= 5
  return average >= 5;
}

// Example usage:
console.log(hasPassed([4, 7, 5, 9, 8, 2])); // Output: true
console.log(hasPassed([4, 7, 5, 3, 8, 2])); // Output: false

Complexity Analysis

The time complexity of this approach is O(n) because we iterate through the array once to compute the sum. The space complexity is O(1) because we only use a few extra variables regardless of the input size.

Edge Cases

Consider the following edge cases:

Testing

To test the solution comprehensively, consider the following test cases:

// Test cases
console.log(hasPassed([])); // Output: false (edge case: empty array)
console.log(hasPassed([5, 5, 5, 5, 5])); // Output: true (all grades are the same)
console.log(hasPassed([4.5, 5.5, 6.5, 7.5])); // Output: true (decimal values)
console.log(hasPassed([1, 2, 3, 4, 5])); // Output: false (average < 5)
console.log(hasPassed([10, 10, 10, 10, 10])); // Output: true (average > 5)

Thinking and Problem-Solving Tips

When approaching such problems, it's important to:

Conclusion

In this blog post, we discussed how to determine if a student has passed based on their grades. We broke down the problem, discussed a step-by-step approach, implemented the solution in JavaScript, and analyzed its complexity. Understanding and solving such problems is crucial for developing strong problem-solving skills.

Additional Resources

For further reading and practice, consider the following resources: