Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Count Character Occurrences

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Count Character Occurrences. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

A QA test for a PlayStation 5 system message needs to verify how often a specific character appears in the message text. Given a string and a target character, return the number of exact, case-sensitive occurrences of that character.

Formal Specification

Implement count_character(text, target).

  • text is a string containing the message to inspect.
  • target is a string containing exactly one character, including a space or punctuation mark.
  • Return an integer representing the number of positions in text whose character equals target.
  • Matching is case-sensitive. For example, P and p are different characters.
  • Do not modify the input string.

Constraints

  • 0 <= len(text) <= 10^5
  • len(target) == 1
  • text may contain letters, digits, spaces, punctuation, and Unicode characters
  • Matching is case-sensitive

Function Signature

def count_character(text, target):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output