cswork / libraryOpen practice workspace

Coding questions · Airbnb

Estimate Page-Visit Duration from the Next User Event

Estimate page-visit duration with each user’s next event, deterministic timestamp ties, and null durations for final events.

Estimate the time spent on each page visit from the next recorded page event for the same user.

Input Table

page_events

ColumnTypeMeaning
event_idINTEGERUnique event identifier; primary key
user_idINTEGERNon-null user identifier
page_idTEXTNon-null page identifier
event_timeTIMESTAMPNon-null event timestamp with whole-second precision

Output Contract

Write one read-only PostgreSQL query returning event_id, user_id, page_id, and duration_seconds for every input event, ordered by event_id ascending.

For this exercise, a user's next page event ends the current page visit. Define the next event by ascending (event_time, event_id) within that user. duration_seconds is the elapsed number of seconds from the current event to that next event. If there is no next event for the user, return SQL NULL for the duration.

Example

event_iduser_idpage_idevent_time
110home2026-01-01 10:00:00
210search2026-01-01 10:00:12
320home2026-01-01 10:00:04
410detail2026-01-01 10:00:12

Expected result:

event_iduser_idpage_idduration_seconds
110home12
210search0
320homeNULL
410detailNULL

Constraints and Clarifications

These are explicit measurement assumptions: there are no session or tab identifiers, no inactivity cutoff, and no additional page-exit events. Use the next event for the same user even across a long gap. The result is a next-event estimate, not proof of uninterrupted attention to a page.

  • Two events for one user may have equal timestamps; the smaller event_id comes first.
  • A repeated visit to the same page still creates a separate row.
  • Events for different users never end one another's visits.
  • Retain each user's final event with a null duration rather than assuming a zero-length visit.

Hint: Look at the neighboring event within a user

The duration belongs to the current row, but its ending timestamp comes from a later row. Establish the partition and ordering before subtracting timestamps.

Page Visit Duration from the Next Page Event

Table page_events

columntypeconstraints
event_idINTEGERPRIMARY KEY
user_idINTEGERNOT NULL
page_idTEXTNOT NULL
event_timeTIMESTAMPNOT NULL

Sample data:

event_idevent_timepage_iduser_id
12026-03-02 09:00:00home101
22026-03-02 09:00:05home202
32026-03-02 09:03:00checkout202
42026-03-02 09:00:45search101
52026-03-02 09:01:00pricing303
62026-03-02 09:02:10product_42101
82026-03-02 09:01:30product_17202
102026-03-02 09:02:40search101
112026-03-02 09:05:00product_42101
122026-03-02 09:01:30cart202
132026-03-02 14:05:00home101
142026-03-02 23:59:50docs404
152026-03-03 00:00:20docs404

Expected output

event_iduser_idpage_idduration_seconds
1101home45
2202home85
3202checkoutNULL
4101search85
5303pricingNULL
6101product_4230
8202product_170
10101search140
11101product_4218000
12202cart90
13101homeNULL
14404docs30
15404docsNULL

Hints

  1. The duration belongs to the current row, but its ending timestamp comes from a later row. Establish the partition and ordering before subtracting timestamps.