KatInterview

First Unique Character and Highest Class Average

StringsAuto-graded · 18 test cases
MediumAuto-gradedStringshash-mapstring-manipulationaggregation

Last updated

Tests hash-based frequency counting for strings and grouped numeric aggregation with exact average comparison.

You are given two independent tasks to solve in a single function.

Task 1: First Unique Character

Given a string s, find the first character that appears exactly once in the entire string. Return that character as a string.

  • If no character appears exactly once, return an empty string "".
  • Characters are case-sensitive: 'a' and 'A' are distinct.
  • The string may contain any ASCII characters including spaces and punctuation.

Task 2: Highest Average Score

You are given a list of score records, where each record is a two-element list [name, score] containing a student name (string) and their score (integer).

Calculate the average score for each student across all their records, then return the highest average among all students as a float.

  • A student may have multiple score records; compute their mean score.
  • Scores can be negative, zero, or positive integers.
  • Each student is guaranteed to have at least one score record.
  • When comparing averages, use exact arithmetic (avoid floating-point comparison errors).

Function Signature

Your function receives both inputs and returns both results as a two-element list: [first_unique_char, highest_avg].

Constraints

  • 1 ≤ len(s) ≤ 100,000
  • 1 ≤ len(records) ≤ 100,000
  • Each score is an integer in the range [-1,000,000, 1,000,000]
  • Student names are non-empty strings

Examples

Example 1

Input: first_unique_and_best_average('swiss', [['Alice', 80], ['Bob', 90], ['Alice', 100]])

Output: ['w', 90]

Example 2

Input: first_unique_and_best_average('aabb', [['A', 70], ['B', 60], ['A', 50]])

Output: ['', 60]

Hints

Hint 1

For the string task, maintain character frequencies and scan the original string in order to preserve position.

Hint 2

For the scoring task, aggregate totals and counts per student, then compute averages only when finding the maximum.

Hint 3

Be careful with negative scores when tracking the maximum average—don't initialize to zero.