Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Count Character Frequencies

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

Your question is Count Character Frequencies. 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

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output