Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Most Frequent Character

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

Your question is Most Frequent Character. 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

Quantiphi's language-processing pipelines may need a quick summary of incoming text. Write a function that returns the character occurring most frequently in a non-empty string.

Character comparison is case-sensitive, and every character counts, including spaces, punctuation, and digits. If multiple characters have the same highest frequency, return the one that appears first in the original string.

Formal Specification

  • Input: A non-empty Python string s.
  • Output: A one-character string containing the most frequent character in s.
  • The input may contain letters, digits, whitespace, punctuation, or Unicode characters.

Constraints

  • 1 <= len(s) <= 10^5
  • The input may contain letters, digits, whitespace, punctuation, or Unicode characters.
  • Character matching is case-sensitive.
  • If frequencies tie, return the character with the earliest index in the string.

Function Signature

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