← TikTok Interview Insights

TikTok·Data Scientist·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

TikTok data scientist interview with a heavy SQL focus. Three questions, all interconnected around the same dataset, which I wasn't expecting. Felt more like a take-home problem crammed into a live session.

Questions Asked (3)

Q1

Write an SQL query to compute a 7-day rolling average of daily unique viewers for every post, ordered by view date.

Product Analytics & MetricsData Modeling
Author's notes

The window function part I got right, COUNT(DISTINCT user_id) over a RANGE between 6 preceding and current row.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the grain: compute daily unique viewers per post, then apply a 7-day rolling average over those daily counts. Use a window function with ROWS BETWEEN 6 PRECEDING AND CURRENT ROW, partitioned by post and ordered by date, and handle missing days by generating a date spine or using RANGE with date arithmetic.

Pro tip: Mention that you'd confirm whether the rolling average should include days with zero viewers (which affects the denominator) and whether the window is calendar-based or event-based—this shows you think about metric definitions, not just syntax.

1. Clarify the metric and grain

Define 'daily unique viewers' as COUNT(DISTINCT viewer_id) per post per day, and confirm whether the rolling average is over calendar days or active days.

2. Aggregate to daily unique viewers

Write a subquery or CTE that groups by post_id and view_date, counting distinct viewers to get the daily metric.

3. Handle missing dates

Generate a date spine for each post (or use a calendar table) and left join the daily counts so that days with zero viewers are included as 0.

4. Compute the rolling average

Use AVG(daily_unique_viewers) OVER (PARTITION BY post_id ORDER BY view_date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) to get the 7-day rolling average.

5. Order and present results

Select post_id, view_date, daily_unique_viewers, and rolling_avg, then order by view_date (and optionally post_id) as requested.

Key Points to Mention

  • Use COUNT(DISTINCT viewer_id) for daily unique viewers.
  • Window function with PARTITION BY post_id ORDER BY view_date and ROWS BETWEEN 6 PRECEDING AND CURRENT ROW.
  • Need to handle missing dates (date spine or calendar table) to avoid incorrect averages.
  • Decide whether to include days with zero viewers in the rolling window.
  • Consider performance implications: pre-aggregate daily counts before windowing.
  • Mention that the rolling average should be computed after daily aggregation, not directly on raw event data.

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

Q2

Given a search term like 'Apple', write a query to return all post IDs where the hashtags column contains that term, case-insensitively.

Data Modeling
Author's notes

Easier than the first question.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the schema and data format, then propose a query using case-insensitive pattern matching (e.g., ILIKE or LOWER with LIKE) with proper wildcards to match hashtags containing the term. Discuss performance considerations and edge cases like partial matches and delimiters.

Pro tip: Mention that hashtags are often stored as delimited strings, so using wildcards on both sides can cause false positives (e.g., 'Apple' matching 'Pineapple'); suggest using delimiter-aware patterns or a normalized hashtag table for production.

1. Clarify Requirements and Schema

Ask about the table structure, data type of the hashtags column, and whether hashtags are stored as a single string or an array. Confirm the expected output (post IDs) and case-insensitivity.

2. Choose Case-Insensitive Matching Method

Select an appropriate SQL function based on the database (e.g., ILIKE in PostgreSQL, LOWER with LIKE in MySQL). Explain why case-insensitivity is needed and how to achieve it.

3. Construct the Query with Wildcards

Write a query that uses wildcards (e.g., '%apple%') to match the term anywhere in the hashtags string. Ensure the pattern is case-insensitive.

4. Address Edge Cases and Performance

Discuss potential false positives (e.g., 'pineapple' matching 'apple') and suggest delimiter-aware patterns (e.g., '%#apple#%' or using string functions). Mention performance implications of leading wildcards and possible indexing strategies.

5. Provide the Final Query and Explain

Present the SQL query clearly, explaining each part. Optionally, mention alternative approaches like full-text search or normalized tables for scalability.

Key Points to Mention

  • Case-insensitive matching techniques (ILIKE, LOWER/LIKE, COLLATE)
  • Wildcard usage and potential false positives with substring matching
  • Delimiter-aware matching to avoid partial hashtag matches
  • Performance considerations: leading wildcards prevent index usage, suggest alternatives
  • Database-specific syntax and functions (e.g., PostgreSQL vs MySQL)
  • Scalability: normalizing hashtags into a separate table for efficient queries

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

Q3

What is the logical execution order of SQL clauses, and why does understanding that order matter for debugging or optimization?

Data ModelingTechnical Trade-offs
Author's notes

FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clearly stating the logical order of SQL clauses, then explain how this order differs from the written syntax. Use concrete examples to show how this knowledge helps in debugging unexpected results and optimizing query performance, especially in large-scale data environments like TikTok.

Pro tip: Mention that window functions are evaluated after WHERE but before ORDER BY, and that this explains why you can't use them in WHERE—a detail that often trips up even experienced practitioners.

1. State the Logical Order

List the logical execution order: FROM/JOIN, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, LIMIT/OFFSET. Emphasize that this is the order in which the database engine processes the query, not the order in which it is written.

2. Contrast with Written Syntax

Explain that SQL is written in a different order (SELECT first) for readability, but the logical order determines what data is available at each stage. This mismatch is a common source of confusion.

3. Debugging Implications

Give examples of how this order affects debugging: e.g., why you can't use a column alias in WHERE (because SELECT is evaluated after WHERE), or why filtering in WHERE before GROUP BY reduces the data processed.

4. Optimization Implications

Discuss how understanding the order helps optimize queries: e.g., pushing filters early (WHERE before JOIN), using HAVING only when necessary, and leveraging indexes effectively.

5. Real-World Example

Provide a concrete example from a data science context, such as calculating user engagement metrics, to illustrate how the logical order impacts query results and performance.

Key Points to Mention

  • Logical order: FROM/JOIN → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT/OFFSET
  • Column aliases defined in SELECT are not available in WHERE because WHERE is evaluated before SELECT.
  • Window functions are evaluated after WHERE, GROUP BY, and HAVING but before ORDER BY and LIMIT.
  • Filtering early with WHERE reduces the amount of data processed in later stages, improving performance.
  • HAVING is used to filter after aggregation, while WHERE filters before aggregation.
  • Understanding the order helps avoid common pitfalls like using aggregate functions in WHERE or misplacing conditions.

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