Pandas interview question: detect dirty product names by group

EasyStrings and text

Problem

Shared data rules

For each “category — is_active” pair, count products and how many have a dirty name. Also find the share of dirty ones. Pairs with no dirty names stay in the answer with 0.

Formulas

Example

Input: products
product_idproduct_namecategoryis_activeRow note
1Whole milk 1 qtDairy1
2 Oat milk 1 qtDairy1← leading spaces
3SOUR CREAM 16 OZDairy1← all caps
4Yukon Gold potatoes 2 lbProduce1← capital inside — fine
5quinoa 28 ozPantry1← all lowercase
6Jasmine rice 2 lbPantry1← two spaces inside
7Clover honey 12 ozPantry0
Output
categoryis_activeproductsdirty_productsdirty_share
Dairy1320.6667
Produce1100
Pantry1221
Pantry0100

Dairy on sale: 2 of 3 names are dirty — 0.6667. Produce · 1 and Pantry · 0 have none, so their rows show 0.

Solve it free

Your code runs on real pandas in your browser. No sign-up needed.

Skills it tests

More interview questions

All questions