PPractick

DSA interview question

If / Else-If Ladder: Grade Calculator

Easy · Algorithms · Topics: language basics

The problem

Your school's result portal needs a function that turns a student's marks (0 to 100) into a grade. The rules from the exam office are: 90–100 → A, 70–89 → B, 50–69 → C, 35–49 → D, and below 35 → Fail. Marks outside 0–100 come from typos in data entry and must be reported as Invalid rather than silently graded. Last year's version checked the conditions in the wrong order and gave a student with 95 marks a "B". Write a function that gets every boundary right.

Example: gradeFor(90) → "A", gradeFor(89) → "B", gradeFor(101) → "Invalid".

Function signature

string gradeFor(int marks);   // returns "A", "B", "C", "D", "Fail" or "Invalid"

Test cases

Input marksOutputExplanation
95AInside the A band
90ALower boundary of A is inclusive
89BJust below A
70BLower boundary of B
50CLower boundary of C
35DLower boundary of D
34FailJust below the pass mark
0FailMinimum valid marks
101InvalidAbove the allowed range
-5InvalidBelow the allowed range

(Confirm: every lower boundary is inclusive, marks are whole numbers, and out-of-range input returns "Invalid".)

How interviewers phrase it

Clarifying questions to ask

  1. Are the lower boundaries inclusive? Is exactly 90 an A?
  2. What should happen for marks outside 0–100: return an error value, throw an exception, or clamp?
  3. Can marks be decimals like 89.5? (Then the boundaries need care with floating-point comparison.)
  4. Is the grading scheme fixed, or should it be configurable?

Follow-ups to expect

  1. "Why does the order of the conditions matter?"
  2. "What's the difference between an else-if ladder and several separate ifs?"
  3. "Could you use switch here?"
  4. "How would you make the grading scheme configurable?"
  5. "What if marks can be 89.999?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in