easy
0 views

DBMS Interview Questions – Part 1

Calculate a student's score on a 20-question quiz with positive marks for correct answers and negative marks for incorrect ones.

Understand the Problem

Problem Statement

You are tasked with creating an automated grader for a 20-question multiple-choice quiz on Database Management Systems (DBMS). The quiz has a specific scoring rule:

  • For each correct answer, a student gets 4 points.
  • For each incorrect answer, a student loses 1 point (i.e., gets -1 point).
  • If a question is not attempted (represented by the character 'N'), it gets 0 points.

You will be given a string of 20 characters representing a student's answers. The correct answers for the 20 questions are fixed. Your program must calculate and print the total score based on the provided answers.

Constraints

  • The input will be a single string of exactly 20 characters.
  • Each character in the input string will be one of the uppercase letters: 'A', 'B', 'C', 'D', or 'N'.
  • The number of questions is always 20.

Examples

Example 1
Input
CBACACBCBCCCBCDACCDA
Output
80
Explanation

The student answered all 20 questions correctly. Each correct answer is worth 4 points. Therefore, the total score is 20 * 4 = 80.

Example 2
Input
BBBBBBBBBBBBBBBBBBBB
Output
5
Explanation

The student answered 5 questions correctly (questions 2, 7, 9, 12, and 14). This gives 5 * 4 = 20 points. The remaining 15 questions were answered incorrectly. This gives 15 * (-1) = -15 points. The total score is 20 - 15 = 5.

Example 3
Input
CBANACBCBCCCBCNACCDA
Output
72
Explanation

The student answered 18 questions correctly, earning 18 * 4 = 72 points. Two questions (4 and 16) were not attempted ('N'), earning 0 points for each. There were no incorrect answers. The total score is 72 + 0 = 72.

Solution

#include <stdio.h>
#include <string.h>

int main() {
    char correct_answers[] = "CBACACBCBCCCBCDACCDA";
    char student_answers[21];
    int score = 0;

    // Read the student's answers
    scanf("%20s", student_answers);

    for (int i = 0; i < 20; i++) {
        if (student_answers[i] == 'N') {
            // No points for not attempted questions
            continue;
        } else if (student_answers[i] == correct_answers[i]) {
            // +4 for a correct answer
            score += 4;
        } else {
            // -1 for an incorrect answer
            score -= 1;
        }
    }

    printf("%d\n", score);

    return 0;
}
Time:O(1)
Space:O(1)
Approach:

The C solution implements the grading logic using basic string and loop operations.

  1. Includes: We include stdio.h for input/output functions (scanf, printf) and string.h though it's not strictly necessary for this logic but good practice for string manipulations.
  2. Data Storage: A character array correct_answers is initialized with the solution key. Another array student_answers of size 21 (20 for answers + 1 for null terminator) is declared to store user input. An integer score is initialized to 0.
  3. Input: scanf("%20s", student_answers) reads exactly 20 characters from the standard input into the student_answers array.
  4. Processing Loop: A for loop iterates 20 times, from i = 0 to 19.
  5. Scoring Logic: Inside the loop, it first checks if the answer is 'N'. If so, it uses continue to skip to the next iteration. Otherwise, it checks if the student's answer matches the correct one. If they match, 4 is added to score. If they don't, 1 is subtracted.
  6. Output: Finally, printf("%d\n", score) prints the final calculated score followed by a newline.

Visual Explanation

Loading diagram...