Quick Overview

Find the most frequent lowercase character in a large string. Resolve equal frequencies by returning the lexicographically smallest character and account for million-character inputs.

Find the Most Frequent Character with a Tie-Breaker

Company: Squarepoint

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: HR Screen

# Find the Most Frequent Character with a Tie-Breaker Implement `most_frequent_character(text)`. `text` is a non-empty string of lowercase English letters. Return the character that appears most often. If multiple characters share the highest frequency, return the lexicographically smallest one. ## Examples - `most_frequent_character("banana")` returns `"a"`. - `most_frequent_character("bbaacc")` returns `"a"` because `a`, `b`, and `c` each appear twice. - `most_frequent_character("z")` returns `"z"`. ## Constraints - `1 <= len(text) <= 1_000_000` - Every character is between `a` and `z`.

Quick Answer: Find the most frequent lowercase character in a large string. Resolve equal frequencies by returning the lexicographically smallest character and account for million-character inputs.

Implement most_frequent_character(text). The non-empty input contains only lowercase English letters. Return the most frequent character; if frequencies tie, return the lexicographically smallest tied character.

Constraints

  • 1 <= len(text) <= 1,000,000
  • text contains only a through z.

Examples

Input: ('z',)

Expected Output: 'z'

Explanation: Exercises frequency counting and the lexicographic tie rule.

Input: ('banana',)

Expected Output: 'a'

Explanation: Exercises frequency counting and the lexicographic tie rule.

Hints

  1. A fixed array of 26 counters is enough.
  2. Scan candidate letters in ascending order and replace the answer only for a strictly larger count.

Loading coding console...