← LinkedIn Interview Insights

LinkedIn·Data Scientist·Technical Phone Screen·Intermediate

Intermediate
Sep 2023

Summary

LinkedIn Data Scientist interview with a SQL question focused on aggregating user activity. Pretty standard stuff, nothing that should trip you up if you've done any analytics work before.

Questions Asked (1)

Q1

Given a posts table with columns for post id, user id, and creation timestamp, write a SQL query that returns each user and their post count over the past 30 days, sorted by post count descending.

Product Analytics & MetricsData Modeling
Author's notes

Straightforward aggregation question.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the table schema and the definition of 'past 30 days' (e.g., relative to current date or a specific date). Then write a SQL query that filters posts within the last 30 days, groups by user, counts posts, and orders by count descending. Consider edge cases like users with zero posts and whether to include them.

Pro tip: Mention that in a real-world scenario, you might need to handle time zones and ensure the date filter is efficient by using an index on the creation timestamp. Also, consider if the metric should include all users or only active users, as this impacts the query and business interpretation.

1. Clarify requirements and schema

Ask about the exact table name, column names, and whether 'past 30 days' is relative to today or a specific date. Confirm if users with zero posts should be included.

2. Filter posts for the last 30 days

Use a WHERE clause with a date function (e.g., creation_timestamp >= CURRENT_DATE - INTERVAL '30 days') to select only recent posts.

3. Group by user and count posts

Use GROUP BY user_id and COUNT(*) to get the number of posts per user. If including users with zero posts, use a LEFT JOIN from a users table.

4. Sort and format output

Order the results by post count descending. Optionally, include the user_id and post_count in the SELECT clause.

5. Validate and discuss edge cases

Check for NULLs, time zone considerations, and performance implications. Discuss how the query would scale and any assumptions made.

Key Points to Mention

  • Use of date functions and interval arithmetic to define the 30-day window.
  • Handling of users with zero posts (LEFT JOIN vs. INNER JOIN).
  • Importance of indexing on the creation timestamp for query performance.
  • Consideration of time zones and whether to use UTC or local time.
  • Clarifying whether 'past 30 days' includes today or is a rolling window.
  • Potential need to join with a users table to get user details or include all users.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.