Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Debugging Code Snippets

MediumTechnical Fundamentals00:00
Practice interviewer
In session
5 left
00:00

Your question is Debugging Code Snippets. Take a moment with it on the right.

Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).

You need to log in / sign up to chat or submit.

Problem

Anudip's learning platform team maintains a small utility that grades a student's quiz attempt and updates their progress record. Here's the function exactly as it's checked in today.

def grade_quiz(answers, correct_answers, passing_score=0.6, history=[]):
    score = 0
    for i in range(len(answers) - 1):
        if answers[i] == correct_answers[i]:
            score = score + 1

    percentage = score / len(correct_answers)
    passed = percentage == passing_score or percentage > passing_score

    history.append(percentage)

    if passed:
        status = "PASS"
    if not passed:
        status = "FAIL"

    return {"score": score, "percentage": percentage, "status": status, "history": history}

Can you identify the error in this block of code? Walk through what happens when a student gets every answer right, and when the quiz itself has an empty correct_answers list.