Count a Character in a String

Read the full interview experience this question came from →

Quick Overview

Count case-sensitive occurrences of a character in a string, including empty input and repeated matches.

Count a Character in a String

Company: Discord

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Count a Character in a String Implement `number_of_character(string, char)`. - `string` is the input string. - `char` is the single character whose occurrences you must count. Return an integer giving the number of times `char` appears in `string`. Comparison is case-sensitive: uppercase and lowercase forms are different characters. Count every matching occurrence, including repeated adjacent occurrences. Return zero if the input string contains no occurrence of `char`, including when the input string is empty. The returned count is a scalar, so its value is the complete output.

Overview: Count case-sensitive occurrences of a character in a string, including empty input and repeated matches.

Read the full Discord Data Scientist interview experience this question came from

|Home/Coding & Algorithms/Discord
Discord logo
Discord
Sep 26, 2026
mediumData ScientistTechnical ScreenCoding & Algorithms
0
0

Count a Character in a String

Implement number_of_character(string, char).

  • string is the input string.
  • char is the single character whose occurrences you must count.

Return an integer giving the number of times char appears in string. Comparison is case-sensitive: uppercase and lowercase forms are different characters. Count every matching occurrence, including repeated adjacent occurrences.

Return zero if the input string contains no occurrence of char, including when the input string is empty. The returned count is a scalar, so its value is the complete output.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...