Skip to main content

Grade from Marks

EasyProblem

Description

Given marks out of 100, return the letter grade they earn:

MarksGrade
90 to 100A
80 to 89B
70 to 79C
60 to 69D
0 to 59F

Examples

Example 1

Input: marks = 95

Output: "A"

Explanation: 95 is 90 or more, the top band.

Example 2

Input: marks = 80

Output: "B"

Explanation: 80 is the lowest mark in the B band, 80 to 89.

Example 3

Input: marks = 59

Output: "F"

Explanation: 59 is just below 60, where D starts.

Example 4

Input: marks = 100

Output: "A"

Explanation: Full marks is still an A.

Constraints

  • 0 ≤ marks ≤ 100

Editorial

Approach - Test the Bands from the Top

Intuition

Test the highest band first. By the time a test for B runs, the A test has already failed, so the marks are below 90 and the B test only needs its lower edge: marks >= 80. The same holds all the way down, and whatever is left after the D test is an F.

Testing in any other order needs both edges in each test, like marks >= 80 and marks < 90.

Step-by-Step Explanation

Let's trace Example 2, marks = 80:

  1. Is 80 ≥ 90? No.
  2. Is 80 ≥ 80? Yes: the grade is B.

From the Top — Each band's lower edge, with its grade below it, tested in order until one is met.

Algorithm

  1. If marks >= 90, return "A".
  2. Otherwise, if marks >= 80, return "B"; then 70 for "C" and 60 for "D".
  3. Otherwise return "F".

Code

class Solution {
public:
    string gradeFromMarks(int marks) {
        if (marks >= 90) return "A";
        if (marks >= 80) return "B";
        if (marks >= 70) return "C";
        if (marks >= 60) return "D";
        return "F";
    }
};
class Solution:
    def gradeFromMarks(self, marks: int) -> str:
        if marks >= 90:
            return "A"
        if marks >= 80:
            return "B"
        if marks >= 70:
            return "C"
        if marks >= 60:
            return "D"
        return "F"
class Solution {
    public String gradeFromMarks(int marks) {
        if (marks >= 90) return "A";
        if (marks >= 80) return "B";
        if (marks >= 70) return "C";
        if (marks >= 60) return "D";
        return "F";
    }
}
var gradeFromMarks = function(marks) {
    if (marks >= 90) return "A";
    if (marks >= 80) return "B";
    if (marks >= 70) return "C";
    if (marks >= 60) return "D";
    return "F";
};

Complexity Analysis

Time Complexity: O(1)

At most four tests, whatever the marks.

Space Complexity: O(1)

Nothing is stored.

Common Mistakes

Each of these looks right on a quick read, and the tests still reject it. The outputs below come from running the code.

Testing with >. if marks > 90:

On marks = 90 it returns "B"; the answer is "A". Each band starts at its lower edge: 90 is an A. marks > 90 leaves 90 for the next test down, so every edge mark gets the grade below.

Testing the lowest band first. if marks >= 60:

On marks = 95 it returns "D"; the answer is "A". The first test that passes decides the grade, and 95 passes marks >= 60 too. Checked from the bottom, every pass is a D; check from the top, or give each band both of its edges.

Leaving out a band. return "F"

On marks = 65 it returns "F"; the answer is "D". Five grades need four tests before the last return. With the D test missing, 60 to 69 fall through to F.

Where This Pattern Is Taught

This problem practises a chain of conditions where the order of the tests does part of the work. It's taught in Python for DSA, Java for DSA, C++ for DSA and JavaScript for DSA.