Label Each Day's Amazon Shoppers as New or Returning in Pandas

Last updated: November 29, 2026

Quick Overview

Given a log of visit timestamps per user, return for each day the number of distinct users visiting for the first time ever and the number of returning users.

Amazon
Data Manipulation (SQL/Python)
Data Scientist
Amazon
November 29, 2026
Data Scientist
Online Assessment
Data Manipulation (SQL/Python)
Easy

0

0

0 solved


Given a log of visit timestamps per user, return for each day the number of distinct users visiting for the first time ever and the number of returning users.

An easy pandas item in the style of Amazon online assessments for analytics roles. The interviewer checks deduplication to user-day, a groupby transform to find each user's first day, and that the two counts add up to daily active users.

What the Interviewer Expects
  • Normalize timestamps to dates
  • Deduplicate to one row per user per day
  • Use groupby transform('min') to attach the first visit date
  • Count new and returning per day with named aggregation
  • Confirm new plus returning equals daily distinct users
Key Topics to Cover
pandas
groupby transform
deduplication
new vs returning
daily active users
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you define returning as visited in the previous 30 days instead of ever?
  • How would you handle data that starts mid-history so everyone looks new on day one?
  • How would you do this in SQL?
  • How would you compute the share of returning users week over week?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

Input: DataFrame visits with user_id and visit_ts. A user is new on the day of their first ever visit in the data and returning on any later day. Several visits on the first day still count as o...

Approach
  1. Convert to date with dt.normalize().
  2. Drop duplicate user-day pairs; otherwise a user visiting twice on day one is counted twice.
  3. groupby('user_id')['visit_date'].transform('min') puts the...

Submit Your Answer
Markdown supported