Mode Finder: Smallest on Tie ๐ŸŒฑ
README.md
main.py
notes.txt
Output
Tests

Mode Finder: Smallest on Tie

๐ŸŒฑ Beginner Project 3836113832

๐Ÿฆ„ Mode Finder: Smallest on Tie

Difficulty: Beginner Tags: hashmap, statistics, frequency-counting Series: CS 101


Problem

In the mystical realm of statistics, the mode is the value that appears most frequently. Your task is to find the mode in a list of magical numbers.

Given a list of integers, return the mode (most frequent value). If multiple values have the same highest frequency (a tie), return the smallest value among them. If the list is empty, return None.

Real-World Application

Mode finding with tiebreakers appears in: - Data analysis - finding the most common value in datasets - Recommendation systems - identifying popular items, breaking ties by rating - Voting systems - determining winners in elections with tiebreaker rules - Quality control - finding the most common defect type - Survey analysis - identifying the most popular response - Caching algorithms - identifying the most accessed items

Input

data = list[int]  # List of integers (may be empty)

Output

Optional[int]  # The mode, or None if list is empty

Constraints

  • List length โ‰ค 100,000
  • Values can be negative, zero, or positive integers
  • Empty list returns None

Examples

Example 1: Clear Mode

Input: [1, 2, 2, 3]

Output: 2

Explanation: - Frequencies: {1: 1, 2: 2, 3: 1} - 2 appears most frequently (2 times) - Mode = 2

Example 2: Tie (Return Smallest)

Input: [2, 2, 1, 1]

Output: 1

Explanation: - Frequencies: {1: 2, 2: 2} - Both 1 and 2 appear 2 times (tied) - Return smallest: 1

Example 3: Empty List

Input: []

Output: None

Explanation: - No elements, no mode

Example 4: All Same Frequency

Input: [5, 3, 8, 1]

Output: 1

Explanation: - Frequencies: {5: 1, 3: 1, 8: 1, 1: 1} - All tied with frequency 1 - Return smallest: 1

Example 5: Single Element

Input: [42]

Output: 42

Explanation: - Only one element, it's the mode

Example 6: Negative Numbers

Input: [-5, -5, -2, 3]

Output: -5

Explanation: - Frequencies: {-5: 2, -2: 1, 3: 1} - -5 appears most frequently (2 times) - Mode = -5

Example 7: Multiple Ties

Input: [10, 10, 5, 5, 1, 1]

Output: 1

Explanation: - Frequencies: {10: 2, 5: 2, 1: 2} - All tied with frequency 2 - Return smallest: 1


What You'll Learn

  • How to count frequencies using hash maps
  • Implementing tiebreaker logic
  • Tracking "best so far" with multiple criteria
  • Distinguishing between absence of data (None) and valid results

Why This Matters

Interview questions frequently test frequency counting combined with selection criteria. This pattern appears in many real-world problems where you need to find extremes with tiebreaker rules.



Starter Code

def challenge_function(data):
    """
    Find the mode (most frequent value) in a list.
    If multiple values tie for mode, return the smallest.

    Args:
        data: list of integers (may be empty)

    Returns:
        int: the mode, or None if list is empty
    """
    # Your implementation here
    pass
OUTPUT
TESTS
Output
Code execution results displayed hereโ€ฆ
Test Results
Test results displayed hereโ€ฆ