cswork / libraryOpen practice workspace

Coding questions · Roblox

Generate Friendship List with Acceptance Dates Using Pandas

This question evaluates proficiency in data manipulation and relational reasoning using Python/pandas, focusing on transforming event logs into canonical pairwise relationships and extracting activation timestamps.

friend_events

requester_idaccepter_idrequest_dateaccept_date
122024-01-012024-01-02
232024-01-032024-01-05
322024-01-042024-01-05
212024-01-062024-01-07
412024-01-082024-01-09
Scenario

After launching a social feature, product wants a list of confirmed friendships with the date they formed.

Question

Given friend request logs, write Python/pandas that returns each distinct user pair once (smaller id first) and the acceptance date when the friendship became active.

Hints

Filter accepted rows, sort ids, drop_duplicates.

Friendship pairs and start date

You are given a log of friend requests and their acceptance dates. Each row represents a request from one user to another, along with the date it was requested and the date it was accepted. A confirmed friendship exists when there is a non-NULL accept_date. Write an SQL query that returns each distinct friendship pair exactly once (with the smaller user id as user1_id and the larger as user2_id) and the date when the friendship became active, defined as the earliest accept_date across all requests in either direction between the two users.

Table friend_events

columntypeconstraints
requester_idINTEGER
accepter_idINTEGER
request_dateDATE
accept_dateDATE

Sample data:

accept_dateaccepter_idrequest_daterequester_id
2024-01-0222024-01-011
2024-01-0532024-01-032
2024-01-0522024-01-043
2024-01-0712024-01-062
2024-01-0912024-01-084

Expected output

One row per unique friendship pair (user1_id = smaller id, user2_id = larger id) with the earliest acceptance date when the friendship formed.

friends_sinceuser1_iduser2_id
2024-01-0212
2024-01-0914
2024-01-0523

Hints

  1. Filter to accepted requests where accept_date IS NOT NULL.
  2. Canonicalize each pair so the smaller user id is always first.
  3. Group by the canonical pair and take the MIN(accept_date) as the friendship start date.