cswork / libraryOpen practice workspace

Coding questions · Meta

Find posts with >60s unconnected viewing time

This question evaluates a candidate's proficiency in data manipulation and aggregation using SQL (or Python) to compute time-windowed metrics from event data, including filtering by boolean flags and summing durations per post.

Context

You work on a social app where users can view posts. A view can be from a connected user (viewer is friends/connected with the post author) or unconnected (not connected).

Tables

Assume the following schema (UTC timestamps):

post_views

  • view_id STRING (PK)
  • post_id STRING
  • viewer_id STRING
  • view_ts TIMESTAMP (UTC)
  • view_duration_seconds INT
  • is_connected BOOLEAN
    • TRUE if the viewer is connected to the post’s author at the time of view

Task

Write a SQL query to find posts whose total unconnected view time in the last 7 days is greater than 60 seconds.

Requirements

  • Time window: [current_timestamp - 7 days, current_timestamp) in UTC.
  • Only include unconnected views: is_connected = FALSE.
  • Aggregate per post_id.

Output

Return:

  • post_id
  • unconnected_view_seconds_7d (sum of view_duration_seconds over the window)

Order by unconnected_view_seconds_7d descending.

Find posts with >60 seconds of unconnected viewing time in the last 7 days

You are given a table of post view sessions. Each row represents one viewing session for a post by a viewer, with the number of seconds watched and whether the viewer is "connected" (e.g., a friend/follower) to the post creator.

For the date range FROM 2025-05-26 TO 2025-06-01 (inclusive), return all posts whose total unconnected viewing time is strictly greater than 60 seconds.

Return:

  • post_id
  • unconnected_view_seconds_7d (sum of view_seconds where is_connected = 0 within the date range)

Sort by unconnected_view_seconds_7d descending, then post_id ascending.

Table post_view_sessions

columntypeconstraints
session_idINTPRIMARY KEY, NOT NULL
post_idINTNOT NULL
viewer_idINTNOT NULL
view_dateDATENOT NULL
view_secondsINTNOT NULL
is_connectedSMALLINTNOT NULL

Sample data:

is_connectedpost_idsession_idview_dateview_secondsviewer_id
010112025-05-2630501
010122025-05-2740502
110132025-05-2810503
010242025-05-2960504
010252025-06-011505
010362025-05-2661506
010372025-05-25100507
010482025-05-3050508
110492025-05-3111509
1105102025-06-0161510

Expected output

One row per post where the sum of unconnected viewing seconds between 2025-05-26 and 2025-06-01 (inclusive) is > 60. Columns: post_id, unconnected_view_seconds_7d.

post_idunconnected_view_seconds_7d
10170
10261
10361

Hints

  1. Use conditional aggregation for unconnected viewing seconds.
  2. Apply the threshold in HAVING after grouping by post_id.