Skip to content

30. Entry and Exit Pages (MAX_BY / MIN_BY)

Difficulty: Medium · Topics: Aggregations

For each session_id in pageviews, find the entry_page (first page viewed), the exit_page (last page viewed) and pages (number of views).

Return session_id, entry_page, exit_page, pages. Timestamps within a session are unique.

Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.

Sample data

pageviews

session_idtspage
s12024-01-01 10:00:00/home
s12024-01-01 10:01:00/pricing
s12024-01-01 10:05:00/signup
s22024-01-01 11:00:00/blog

Expected output

session_identry_pageexit_pagepages
s1/home/signup3
s2/blog/blog1

Hints

Hint 1You need "the page at the smallest timestamp", not the smallest page.
Hint 2F.min_by("page", "ts") and F.max_by("page", "ts") do exactly that inside one groupBy.
Hint 3Without them you would need a window with row_number or a join back to the min/max timestamp.

PySpark functions you'll practise

Related problems

Browse

Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings

Difficulty: Easy · Medium · Hard · PySpark interview roadmap · All problems