PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question tests a candidate's practical ability to traverse strings and use hash-based data structures for frequency counting while preserving insertion order. It evaluates core algorithmic thinking in the Coding & Algorithms domain, commonly asked to assess proficiency with hash maps and attention to edge cases like case sensitivity.

  • medium
  • Antra
  • Coding & Algorithms
  • Software Engineer

Character Frequency Count in a String

Company: Antra

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Character Frequency Count in a String Given a string `s`, count how many times each character occurs in it and return the per-character frequencies. Process the characters in the **order of first appearance**: the output must list each distinct character the first time it is seen, from left to right, paired with its total number of occurrences across the whole string. The comparison is case-sensitive, so `'A'` and `'a'` are different characters, and whitespace characters (such as a space) count as characters. ### Output Format Return a list of `(character, count)` pairs ordered by each character's first occurrence in `s`. For an empty input string, return an empty list. ### Examples Example 1: - Input: `s = "hello"` - Output: `[('h', 1), ('e', 1), ('l', 2), ('o', 1)]` - Explanation: `'h'` appears first and occurs once; `'e'` next, once; `'l'` next, twice; `'o'` last, once. Example 2: - Input: `s = "aabbbc"` - Output: `[('a', 2), ('b', 3), ('c', 1)]` Example 3: - Input: `s = "AaAa"` - Output: `[('A', 2), ('a', 2)]` - Explanation: Uppercase and lowercase are treated as distinct characters. Example 4: - Input: `s = ""` - Output: `[]` ### Constraints - `0 <= s.length <= 10^5` - `s` consists of printable ASCII characters (character codes 32 through 126), including spaces. ### Required Function Implement a function that takes the string `s` and returns the list of `(character, count)` pairs ordered by first appearance.

Quick Answer: This question tests a candidate's practical ability to traverse strings and use hash-based data structures for frequency counting while preserving insertion order. It evaluates core algorithmic thinking in the Coding & Algorithms domain, commonly asked to assess proficiency with hash maps and attention to edge cases like case sensitivity.

Given a string `s`, count how many times each character occurs in it and return the per-character frequencies. Process the characters in the **order of first appearance**: the output must list each distinct character the first time it is seen, from left to right, paired with its total number of occurrences across the whole string. The comparison is case-sensitive, so `'A'` and `'a'` are different characters, and whitespace characters (such as a space) count as characters. **Output Format:** Return a list of `(character, count)` pairs ordered by each character's first occurrence in `s`. For an empty input string, return an empty list. **Examples:** - `s = "hello"` → `[('h', 1), ('e', 1), ('l', 2), ('o', 1)]` - `s = "aabbbc"` → `[('a', 2), ('b', 3), ('c', 1)]` - `s = "AaAa"` → `[('A', 2), ('a', 2)]` (uppercase and lowercase are distinct) - `s = ""` → `[]`

Constraints

  • 0 <= s.length <= 10^5
  • s consists of printable ASCII characters (codes 32 through 126), including spaces.
  • Comparison is case-sensitive: 'A' and 'a' are different characters.

Examples

Input: ("hello",)

Expected Output: [('h', 1), ('e', 1), ('l', 2), ('o', 1)]

Explanation: 'h' is seen first (1 time), then 'e' (1), then 'l' (2 across the string), then 'o' (1), in first-appearance order.

Input: ("aabbbc",)

Expected Output: [('a', 2), ('b', 3), ('c', 1)]

Explanation: 'a' appears twice, 'b' three times, 'c' once, listed in the order each is first encountered.

Input: ("AaAa",)

Expected Output: [('A', 2), ('a', 2)]

Explanation: Case-sensitive: 'A' and 'a' are distinct, each occurring twice; 'A' is seen first.

Input: ("",)

Expected Output: []

Explanation: Empty input yields an empty list.

Input: ("a",)

Expected Output: [('a', 1)]

Explanation: Single character occurs once.

Input: ("a b a",)

Expected Output: [('a', 2), (' ', 2), ('b', 1)]

Explanation: Whitespace counts as a character: 'a' twice, the space twice, 'b' once, in first-appearance order.

Input: ("112233!!",)

Expected Output: [('1', 2), ('2', 2), ('3', 2), ('!', 2)]

Explanation: Digits and punctuation are counted like any other characters.

Hints

  1. Iterate through the string once, maintaining a count for each character in a hash map.
  2. To preserve first-appearance order, record each character in a separate list the first time you see it (or rely on an insertion-ordered map).
  3. A space and punctuation are characters too — do not skip or normalize them.
Last updated: Jun 24, 2026

Related Coding Questions

  • Longest Substring Without Repeating Characters - Antra (medium)

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.