-
Notifications
You must be signed in to change notification settings - Fork 243
Add Student Grade
JCSU Unit 2 Problem Set 2 (Click for link to problem statements)
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10 mins
- 🛠️ Topics: Dictionary Operations, Conditional Logic
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
- What is the goal of the problem?
- The goal is to update a dictionary of students and grades by adding a grade to an existing student's list or initializing a new student with the grade.
- Are there constraints on input?
- The input dictionary may be empty or already populated.
HAPPY CASE Input: grades = {"Alice": [90, 85], "Bob": [70, 80]} add_student_grade(grades, "Alice", 95) add_student_grade(grades, "Charlie", 88) Output: {"Alice": [90, 85, 95], "Bob": [70, 80], "Charlie": [88]} Explanation: "Alice" receives a new grade of 95, and "Charlie" is added to the dictionary with a grade of 88.
EDGE CASE Input: grades = {} add_student_grade(grades, "Alice", 100) Output: {"Alice": [100]} Explanation: A new dictionary is created with "Alice" and their grade of 100.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For dictionary update problems, we want to consider the following approaches:
-
Conditional Logic: Use
if-else
to check if the key exists and update accordingly. -
Default Dictionary (Alternative): Use
collections.defaultdict
for automatic initialization of lists (not required here).
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
Check if the student already exists in the dictionary. If they do, append the new grade to their list. If they don't, create a new entry with the grade as the first item in their list.
- Check if the
student_name
exists in thegrades
dictionary.- If yes, append the
grade
to the student's list of grades. - If no, add the
student_name
to the dictionary with thegrade
as their first entry in a list.
- If yes, append the
- The function does not need to return a value since the dictionary is modified in place.
Implement the code to solve the algorithm.
def add_student_grade(grades, student_name, grade):
if student_name in grades: # Check if the student already exists in the dictionary
grades[student_name].append(grade) # Append the new grade to their list of grades
else:
grades[student_name] = [grade] # Add a new student with the grade as their first entry
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
- Input: grades = {"Alice": [90, 85], "Bob": [70, 80]} add_student_grade(grades, "Alice", 95) add_student_grade(grades, "Charlie", 88)
- Expected Output: {"Alice": [90, 85, 95], "Bob": [70, 80], "Charlie": [88]}
- Observed Output: {"Alice": [90, 85, 95], "Bob": [70, 80], "Charlie": [88]}
Example 2:
- Input: grades = {} add_student_grade(grades, "Alice", 100)
- Expected Output: {"Alice": [100]}
- Observed Output: {"Alice": [100]}
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume n is the number of students in the dictionary.
- Time Complexity: O(1) on average for dictionary operations (key lookup and insertion).
- Space Complexity: O(1) additional space, as the dictionary is modified in place.