Find maximum-occurring character in a string
Company: SoFi
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Take-home Project
Quick Answer: This question evaluates proficiency in string manipulation and frequency counting, including correct handling of tie-breaking by earliest first occurrence and awareness of character-set distinctions.
Constraints
- 1 <= len(s) <= 2 * 10^5
- Characters are case-sensitive.
- Each printable ASCII character should be treated as a distinct character.
Examples
Input: ("abcaac",)
Expected Output: "a"
Explanation: Frequencies are: a=3, c=2, b=1, so the answer is 'a'.
Input: ("abcd",)
Expected Output: "a"
Explanation: All characters appear once, so the tie is broken by earliest first occurrence. 'a' appears first.
Input: ("aAaA",)
Expected Output: "a"
Explanation: The string is case-sensitive: 'a'=2 and 'A'=2. Since 'a' appears first at index 0, return 'a'.
Input: ("x",)
Expected Output: "x"
Explanation: Single-character edge case: the only character is the answer.
Input: ("112233!!",)
Expected Output: "1"
Explanation: Characters '1', '2', '3', and '!' each appear twice. '1' has the smallest first occurrence index.
Input: ("abbbbaaa",)
Expected Output: "a"
Explanation: 'a' appears 4 times and 'b' appears 4 times. Since 'a' first appears at index 0 and 'b' at index 1, return 'a'.
Hints
- Use a hash map (dictionary) to count how many times each character appears.
- To handle ties correctly, also store the first index where each character appears.