JCSU Unit 2 Problem Set 2 (Click for link to problem statements)
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?
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:
if-else
to check if the key exists and update accordingly.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.
student_name
exists in the grades
dictionary.
grade
to the student's list of grades.student_name
to the dictionary with the grade
as their first entry in a list.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:
Example 2:
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.