cswork / libraryOpen practice workspace

Coding questions · Pinterest

Find top category by video time spent

This question evaluates proficiency in pandas-based data manipulation and aggregation, testing competencies in data cleaning, normalization, mapping, filtering, and computing summary statistics.

Pandas required. You are given a DataFrame df with columns: user_id (int), pin_id (int), pin_type (str), category (str or None), time_spent_sec (numeric). Goal: among video pins, find the canonical category with the highest average time_spent_sec. Requirements:

  • Consider pin_type values case-insensitively and treat 'vedio' as 'video' (data quality issue).
  • Normalize category by lowercasing and stripping whitespace, then map via category_map; if a key is missing after normalization or category is null/empty, map to 'unknown'.
  • Exclude rows where time_spent_sec is null or non-positive.
  • Return a two-field result: top_category (str), avg_time_spent_sec (float, rounded to 2 decimals).

Example inputs

category_map = {
 'home': 'lifestyle',
 'food & drink': 'food',
 'recipe': 'food',
 'travel': 'travel'
}
df (illustrative rows)
user_id | pin_id | pin_type | category | time_spent_sec
1 | 10 | 'video' | 'Food & Drink' | 120
2 | 11 | 'video' | None | 200
3 | 12 | 'static' | 'Home' | 90
4 | 13 | 'video' | 'Recipe' | 240
5 | 14 | 'video' | 'DIY' | 180
6 | 15 | 'vedio' | 'food & drink ' | 60

What is the top_category and its average time among video pins after mapping and cleaning?

Find the top canonical category by average video time spent

You are given engagement data for pins.

Task: Among video pins only, find the canonical category with the highest average time_spent_sec after cleaning and mapping.

Cleaning / mapping rules:

  1. Treat pin_type case-insensitively and fix the data quality issue where 'vedio' should be treated as 'video'.
  2. Normalize category by lowercasing and trimming whitespace.
    • If category is NULL or becomes an empty string after trimming, treat it as missing.
    • Map the normalized category using the category_map table.
    • If there is no matching key in category_map, map the category to 'unknown'.
  3. Exclude rows where time_spent_sec is NULL or time_spent_sec <= 0.

Return exactly two fields:

  • top_category (the canonical category string)
  • avg_time_spent_sec (average time spent for that canonical category among video pins, rounded to 2 decimals)

If there is a tie, return the lexicographically smallest top_category among the tied categories.

Table pin_engagement

columntypeconstraints
user_idINTNOT NULL
pin_idINTPRIMARY KEY, NOT NULL
pin_typeVARCHAR(20)NOT NULL
categoryVARCHAR(100)
time_spent_secDECIMAL(10,2)

Sample data:

categorypin_idpin_typetime_spent_secuser_id
Food & Drink10video1201
NULL11video2002
Home12static903
Recipe13video2404
DIY14video1805
food & drink15vedio606
Travel16Video307
Home17videoNULL8
Home18video-109
Recipe19static30010
20video21011

Table category_map

columntypeconstraints
category_keyVARCHAR(100)PRIMARY KEY, NOT NULL
canonical_categoryVARCHAR(50)NOT NULL

Sample data:

canonical_categorycategory_key
lifestylehome
foodfood & drink
foodrecipe
traveltravel

Expected output

A single-row result with the canonical category that has the highest average time_spent_sec among cleaned video pins, plus that average rounded to 2 decimals.

avg_time_spent_sectop_category
196.67unknown

Hints

  1. Normalize strings with LOWER(TRIM(...)) and treat empty strings as NULL via NULLIF(..., '').
  2. Use a LEFT JOIN to the mapping table and COALESCE to default missing mappings to 'unknown'.
  3. Compute AVG per canonical category, then ORDER BY the average descending and take the top row.