Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Count Character Frequencies
00:00
5 left

Count Character Frequencies

MediumPython

Problem

Iru's message tools need a compact character-frequency summary for analyzing user-entered text. Given a message, count each character and return the results ranked from most frequent to least frequent.

Formal Specification

Implement character_frequencies(text, case_sensitive, include_whitespace).

  • text is a string.
  • case_sensitive is a Boolean. If False, convert alphabetic characters to lowercase before counting.
  • include_whitespace is a Boolean. If False, ignore whitespace characters such as spaces, tabs, and newlines.
  • Return a list of two-element lists, where each element is [character, count].
  • Sort results by decreasing count. If two characters have the same count, preserve the order in which their normalized characters first appeared in the filtered input.
  • Return an empty list when no characters remain after filtering.

Character comparisons use Python string characters, so punctuation, digits, and Unicode characters are valid inputs.

Constraints

  • 0 <= len(text) <= 100,000
  • text may contain ASCII or Unicode characters
  • Whitespace is identified with character.isspace()
  • Results are sorted by decreasing frequency
  • Equal frequencies are ordered by first appearance after filtering and normalization

Function Signature

def character_frequencies(text, case_sensitive, include_whitespace):
Interviewer

Your question is Count Character Frequencies. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.