DSA interview question
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".
string gradeFor(int marks); // returns "A", "B", "C", "D", "Fail" or "Invalid"
Input marks | Output | Explanation |
|---|---|---|
| 95 | A | Inside the A band |
| 90 | A | Lower boundary of A is inclusive |
| 89 | B | Just below A |
| 70 | B | Lower boundary of B |
| 50 | C | Lower boundary of C |
| 35 | D | Lower boundary of D |
| 34 | Fail | Just below the pass mark |
| 0 | Fail | Minimum valid marks |
| 101 | Invalid | Above the allowed range |
| -5 | Invalid | Below the allowed range |
(Confirm: every lower boundary is inclusive, marks are whole numbers, and out-of-range input returns "Invalid".)
ifs?"switch here?"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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in