Subscribers With Two Streams Within Five Days
Problem
You are given one DataFrame `streams` with columns `stream_id`, `listener_id`, and `played_on` (a date). `stream_id` is unique.
Return the `listener_id` of every listener who has any two streams that occurred at most 5 days apart (the two dates differ by 5 days or fewer).
Return the result ordered by `listener_id` ascending.
Input data
Example rows — the live problem includes the full dataset.
streams
| stream_id | listener_id | played_on |
|---|---|---|
| 1 | 100 | 2024-01-01 |
| 2 | 100 | 2024-01-04 |
| 3 | 200 | 2024-01-01 |
| 4 | 200 | 2024-01-20 |
| 5 | 300 | 2024-02-01 |
Expected output
Your answer should return 2 rows with the columns listener_id.
Starter code (Pandas (Python))
import pandas as pd
def streams_within_five_days(streams) -> pd.DataFrame:
# Your code here
return streamsSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock