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
Preprocess Text with Python
00:00
5 left

Preprocess Text with Python

MediumPython

Problem

Capgemini Government Solutions is preparing text features for a document-analysis pipeline. Implement a deterministic preprocessing function that converts raw ASCII text into normalized token frequencies.

For each input string:

  1. Convert letters to lowercase.
  2. Extract tokens consisting only of contiguous ASCII letters and digits. Punctuation and other separators are delimiters.
  3. Remove tokens present in stop_words, which should be matched case-insensitively.
  4. Apply these simple normalization rules, in order: remove ing, ed, es, or a final s when the remaining token has at least three characters. Do not remove a final s when the token ends in ss.
  5. Remove normalized tokens shorter than min_length.
  6. Return a dictionary mapping each remaining token to its frequency. Preserve no information about token positions.

The input text contains only ASCII characters. Stemming is intentionally simple and does not need to handle every English word.

Formal Specification

Implement preprocess_text(text, stop_words, min_length). text is a string, stop_words is a list of strings, and min_length is a positive integer. Return a dictionary from normalized string tokens to integer counts. Return {} when no tokens remain.

Constraints

  • 1 <= len(text) <= 10^5
  • 0 <= len(stop_words) <= 10^4
  • 1 <= min_length <= 20
  • Tokens and stop words contain at most 32 ASCII letters or digits
  • The input text contains only ASCII characters

Function Signature

def preprocess_text(text, stop_words, min_length):
Interviewer

Your question is Preprocess Text with Python. 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.