← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Senior

Senior
Jul 2026

Summary

SQL-heavy technical screen for a Data Engineer role at Meta. One question, pretty deep on window functions and filtering logic. Felt okay during but second-guessed my tie-breaking clause afterward.

Questions Asked (1)

Q1

Given a copies table (copy_id, book_id) and a checkouts table (copy_id, checkout_date, return_date), find the top 3 book_ids that have more than 10 distinct copies and rank them by total borrowed duration (sum of day-level date differences across completed checkouts only), with ties broken by book_id ascending.

Data ModelingAlgorithms & Data Structures
Author's notes

The NULL filter tripped me up first.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Break the problem into two stages: first, compute the total borrowed duration per book by joining copies and checkouts, filtering for completed checkouts (return_date IS NOT NULL), and summing the day-level date differences; second, filter books with more than 10 distinct copies, sort by total duration descending and book_id ascending, and limit to top 3. Use a subquery or CTE to aggregate durations and counts separately, then join or filter as needed.

Pro tip: Explicitly state your assumptions about date difference calculation (e.g., DATEDIFF(day, checkout_date, return_date) in SQL Server or return_date - checkout_date in PostgreSQL) and clarify that 'completed checkouts' means return_date is not null. Also, mention that you would validate the query with edge cases like books with exactly 10 copies or ties in duration.

1. Clarify requirements and assumptions

Confirm the definition of 'total borrowed duration' (sum of day-level differences for completed checkouts only) and 'more than 10 distinct copies' (count of distinct copy_id per book). Ask about date difference semantics (inclusive/exclusive) and handling of NULL return dates.

2. Aggregate checkout durations per book

Join copies and checkouts on copy_id, filter for completed checkouts (return_date IS NOT NULL), and compute the sum of date differences per book_id. Use a CTE or subquery to isolate this aggregation.

3. Count distinct copies per book

From the copies table, count distinct copy_id per book_id to identify books with more than 10 distinct copies. This can be done in a separate CTE or combined with the duration aggregation.

4. Combine, filter, and rank

Join the duration and copy-count results, filter for books with >10 distinct copies, then order by total duration descending and book_id ascending. Limit to top 3.

5. Review and optimize

Check for potential performance issues (e.g., large joins) and suggest indexes on copy_id and book_id. Discuss alternative approaches like window functions if needed.

Key Points to Mention

  • Use of INNER JOIN between copies and checkouts to ensure only valid copy_ids are considered.
  • Filtering completed checkouts with return_date IS NOT NULL.
  • Calculating day-level difference using appropriate date functions (e.g., DATEDIFF, date subtraction).
  • Aggregating with SUM and COUNT(DISTINCT) in separate subqueries or CTEs to avoid incorrect results due to join multiplication.
  • Applying HAVING COUNT(DISTINCT copy_id) > 10 to filter books.
  • Ordering by total_duration DESC, book_id ASC and using LIMIT 3 (or TOP 3).

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