Hone logo
Hone
Problems

Number of Unique Subjects Taught by Each Teacher

This challenge focuses on analyzing data about teachers and the subjects they teach. Given a list of teacher-subject pairings, the goal is to determine the number of unique subjects taught by each teacher. This is a common task in educational data analysis, useful for understanding teacher workload and specialization.

Problem Description

You are given a list of records, where each record represents a teacher and a subject they teach. The input is a list of tuples (or similar data structure depending on the language you choose), where each tuple contains two elements: a teacher's name (string) and a subject name (string). Your task is to process this list and determine the number of distinct subjects taught by each teacher. The output should be a dictionary (or similar data structure) where the keys are teacher names and the values are the number of unique subjects they teach.

Key Requirements:

  • Uniqueness: Ensure that each subject is counted only once per teacher, even if a teacher teaches the same subject multiple times.
  • Data Structure: The output must be a dictionary (or equivalent) mapping teacher names to the count of unique subjects.
  • Handling Missing Data: If a teacher is not present in the input list, they should not be included in the output dictionary.
  • Case Sensitivity: Teacher and subject names are case-sensitive. "Math" and "math" are considered different subjects.

Expected Behavior:

The function should iterate through the input list, track the unique subjects taught by each teacher, and construct the output dictionary accordingly.

Edge Cases to Consider:

  • Empty input list: Should return an empty dictionary.
  • Teacher teaching the same subject multiple times.
  • Multiple teachers teaching the same subject.
  • Teacher names and subject names with varying capitalization.

Examples

Example 1:

Input: [("Alice", "Math"), ("Bob", "Science"), ("Alice", "Physics"), ("Bob", "Science"), ("Charlie", "History")]
Output: {"Alice": 2, "Bob": 1, "Charlie": 1}
Explanation: Alice teaches Math and Physics (2 unique subjects). Bob teaches only Science (1 unique subject). Charlie teaches History (1 unique subject).

Example 2:

Input: [("Alice", "Math"), ("Alice", "Math"), ("Bob", "Science"), ("Charlie", "History"), ("Charlie", "History"), ("David", "English")]
Output: {"Alice": 1, "Bob": 1, "Charlie": 1, "David": 1}
Explanation: Alice teaches Math only once. Bob teaches Science only once. Charlie teaches History only once. David teaches English only once.

Example 3:

Input: []
Output: {}
Explanation: An empty input list results in an empty output dictionary.

Constraints

  • The input list can contain up to 1000 records.
  • Teacher names and subject names are strings with a maximum length of 50 characters.
  • The solution should have a time complexity of O(n), where n is the number of records in the input list. (Using a hash table/dictionary is generally the most efficient approach).
  • The input list will only contain tuples of length 2.

Notes

Consider using a dictionary (or hash map) to efficiently track the unique subjects taught by each teacher. Iterating through the input list once and updating the dictionary as you go is a good strategy. Think about how to handle the case where a teacher is encountered for the first time.

Loading editor...
plaintext