Explode Multi-Valued Interest Tags and Count Users per Tag
Last updated: November 29, 2026
Quick Overview
A profiles DataFrame stores each user's inferred interests as a comma-separated string like 'cooking, travel,Travel'. Return, per country, the five most common interests by distinct user count after normalizing case and whitespace.
Meta
November 29, 20260
0
0 solved
A profiles DataFrame stores each user's inferred interests as a comma-separated string like 'cooking, travel,Travel'. Return, per country, the five most common interests by distinct user count after normalizing case and whitespace.
This shows up early in Meta data science screens as a quick check on string cleaning and the explode pattern, using an audience-insights style dataset. The interviewer watches whether you normalize tags, avoid double counting a user who lists the same interest twice, and handle empty or null tag fields.
What the Interviewer Expects
- Split the string into a list and explode to one row per user and tag
- Normalize with strip and lower so 'Travel' and ' travel' match
- Drop empty strings and nulls produced by trailing commas or missing data
- Count distinct users, not rows, per country and tag
- Take top five per country with a deterministic tie-break
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you compute the lift of each interest in a country versus its global share?
- What if tags were stored as a JSON array column instead of a string?
- How would you do this in Presto with UNNEST?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
Columns: user_id, country, interests (string, may be null or empty, may contain duplicates and odd spacing). Each user appears once in the table. I will return country, interest, users, so...
Approach
Split on commas into lists, explode to long form, normalize each tag, and drop blanks. Then drop duplicate (user_id, interest) pairs so a user who wrote the same tag twice counts once. Group by countr...