Word Frequency Counter
PYTHON coding challenge · Difficulty: medium · Topic: Data Structures · +50 XP
Problem
Function Signature
------------------
def word_frequency(sentences: list[str]) -> dict:
Problem
-------
Given a list of sentences, count how many times
each word appears across all of them.
Rules:
• Convert every word to lowercase
• Split each sentence on whitespace
• Return a dict: {word: count}
• Sort by count DESC, then word ASC
Example 1
---------
Input:
["Spark is fast", "Spark is great",
"Spark runs everywhere"]
Output:
{'spark': 3, 'is': 2, 'everywhere': 1,
'fast': 1, 'great': 1, 'runs': 1}
Example 2
---------
Input:
["hello world", "hello python",
"world is great"]
Output:
{'hello': 2, 'world': 2, 'great': 1,
'is': 1, 'python': 1}
Example 3 (Edge case)
---------------------
Input: []
Output: {}
Constraints
-----------
• 0 <= len(sentences) <= 1000
• Words are separated by single spaces
• Dict order matters: count DESC, then word ASC
Example Input
The data below is already defined — do not redefine it.
`python
sentences = ["Spark is fast", "Spark is great", "Spark runs everywhere"]
`
Expected Output
`
{'spark': 3, 'is': 2, 'everywhere': 1, 'fast': 1, 'great': 1, 'runs': 1}
`
Notes
- Print the result — the grader reads standard output
- Do not redefine the input; it is provided for you
Common mistakes
- Order is part of the answer. The output is compared as printed, so apply the sort before building the dict: count descending, then word ascending.
Counter.most_common()gives count-descending but does not break ties alphabetically -- sort explicitly.
What this PYTHON challenge teaches you
“Word Frequency Counter” is a medium-level PYTHON challenge focused on Data Structures. Working through it gives you hands-on practice with dict, strings, Counter — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- dict
- strings
- Counter
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- This is a Data Structures problem — review the matching python concept.
- Compare your output to the Expected Output — the columns, values and row order must match exactly.
Where this comes up
Variations of this problem have been reported in interviews at Google, Microsoft, Amazon. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.
How to practise it on PySpark.in
Open the challenge, write your Python code in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related PYTHON challenges
- Count Word Occurrences in a List
- Validate Unique User IDs
- Extract Error Codes from Log Strings
- Sum of Values by Key
- Detect Anomalies in Transaction Batches
- Flatten Nested JSON Structure
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The PYTHON environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated medium and covers Data Structures.