← Questions

SQL screening with joins and window functions

0
NotionCoding

A coding interview question asked at Notion. It tests sql, joins and aggregation.

A SQL screen on a document product. Two tables record who opened which page and where each user lives. Write a query for each question below. Each tests one tool: a join with a distinct count, a self-join, a ratio with the right denominator, and a window function.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • users(user_id, country)
  • page_views(page_id, user_id, viewed_at, is_creator, source) — one row per view. is_creator is 1 when the viewer created the page. For other viewers (collaborators), source says how they reached the page: invite, share_link, workspace, ...

Questions:

  1. How do collaborators reach pages? For each source, the number of distinct (page, collaborator) pairs and its share of all pairs.
  2. For a given user, who do they collaborate with most? Two users collaborate on a page when both viewed it. Return the other user with the most shared pages.
  3. Which country collaborates most? For each country, the percentage of its users who viewed at least one page they did not create.
  4. For each user, the first page they opened as a collaborator, and when.

Example

users:       1 US, 2 US, 3 JP, 4 JP
page_views:  p1: 1 (creator), 2 invite, 3 share_link
             p2: 2 (creator), 1 invite, 1 invite (second view)
             p3: 4 (creator)

Q1  invite 2 (66.7%), share_link 1 (33.3%)    # user 1's two views of p2 are one pair
Q2  for user 1: user 2 (2 shared pages)
Q3  US 100.0, JP 50.0                          # user 4 never collaborated
Q4  user 1 -> p2 at 2026-01-05, user 2 -> p1, user 3 -> p1

Constraints

  • page_views has hundreds of millions of rows; a user can view the same page many times.

Clarifying questions

  • Count views, or distinct (page, user) pairs?
  • In question 3, is the denominator every user in the country, or only users who viewed anything?
  • Is is_creator reliable, or should it be derived from a separate pages table?
  • How should ties be reported?

What a strong answer covers

  • SELECT DISTINCT (or COUNT(DISTINCT ...)) before counting, so repeat views don't inflate results.
  • A self-join of distinct (page, user) pairs on page_id with a.user_id <> b.user_id.
  • Question 3 with users as the driving table and a LEFT JOIN to collaborators, so countries with no collaborators still show 0, and 100.0 * to avoid integer division.
  • ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY viewed_at) for question 4, and why MIN(viewed_at) alone can't return the page.
  • Checking each query on a tiny hand-built table.

Follow-ups

  • Rewrite question 2 for every user at once, and say why the self-join gets expensive on very popular pages.
  • Add a 7-day rolling count of collaborator views per page with a window frame.
  • How would you do question 3 in pandas, and where is it easy to get the numerator wrong?

Where it's asked

What it tests

sql · joins · aggregation

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding