← TikTok Interview Insights

TikTok·Data Scientist·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026Remote

Summary

SQL-heavy data science interview at TikTok, all four questions were about querying two streaming tables. Nothing behavioral, just back-to-back SQL with some tricky edge cases around window functions and joins.

Questions Asked (4)

Q1

Write a SQL query to calculate total streamed hours per month, including a solution that handles data spanning multiple years.

Product Analytics & MetricsData Modeling
Author's notes

Pretty straightforward at first glance.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the schema and definitions (e.g., what table stores streaming events, how duration is measured, and what 'month' means). Then write a SQL query that aggregates total streamed hours per month, using date functions to extract year and month, and ensure it handles multi-year data by grouping on both year and month or by using a date truncation function. Finally, discuss edge cases like time zones, incomplete months, and performance considerations.

Pro tip: Mention that you would use a date truncation function (e.g., DATE_TRUNC('month', event_date)) to group by month, which automatically handles multiple years and is more efficient than extracting year and month separately. Also, note that for large datasets like TikTok's, you might pre-aggregate or partition by date to improve performance.

1. Clarify requirements and schema

Ask about the table structure, the definition of 'streamed hours' (e.g., sum of watch time per user per video), and how months should be defined (calendar month, time zone).

2. Identify the aggregation key

Determine that you need to group by month and year. Use a date truncation function or extract year and month to create a unique month identifier across years.

3. Write the SQL query

Construct a query that sums the streamed hours (or duration) and groups by the month identifier. Ensure the query includes all necessary filters (e.g., valid events).

4. Handle multi-year data

Explain that grouping by year and month (or using DATE_TRUNC) ensures months from different years are separate. Optionally, format the output to show 'YYYY-MM' for clarity.

5. Discuss edge cases and optimizations

Mention handling of time zones, incomplete months, and performance tips like partitioning or indexing on date columns.

Key Points to Mention

  • Use of DATE_TRUNC or EXTRACT(YEAR FROM ...) and EXTRACT(MONTH FROM ...) to group by month across years.
  • Definition of 'streamed hours': sum of watch time per event, possibly requiring conversion from seconds to hours.
  • Handling of time zones: ensure dates are in a consistent time zone (e.g., UTC) before grouping.
  • Inclusion of all months, even those with zero streams, if required (e.g., using a calendar table or LEFT JOIN).
  • Performance considerations: partitioning by date, using columnar storage, or pre-aggregating for large datasets.
  • Output format: presenting results as 'YYYY-MM' or separate year and month columns for readability.

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

Q2

For each streamer, calculate their total streaming duration and the ratio of time spent in a specific category to their total duration. How do you handle queries when the category name is case-sensitive or passed as a keyword?

Product Analytics & MetricsData Modeling
Author's notes

I fumbled a bit on the case sensitivity piece.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the schema and business context, then outline a SQL-based solution using aggregation and conditional logic. Emphasize handling case sensitivity and parameterization to ensure robustness and reusability.

Pro tip: Mention that case sensitivity depends on the database collation and show awareness of performance implications when filtering on category. Also, suggest using a parameterized query or a CTE to avoid hardcoding the category name.

1. Clarify Requirements and Schema

Ask about the table structure, data types, and whether the category name is case-sensitive in the database. Confirm if the query needs to be dynamic or if the category is fixed.

2. Design the Aggregation Query

Write a SQL query that groups by streamer, sums the duration for total streaming time, and conditionally sums duration for the specific category. Use a CASE statement or FILTER clause to isolate the category.

3. Handle Case Sensitivity and Parameterization

Use LOWER() or UPPER() on both the column and the input parameter to ensure case-insensitive matching, or rely on the database's collation settings. For parameterization, use a prepared statement or a variable to pass the category name safely.

4. Compute the Ratio and Validate

Calculate the ratio as category_duration / total_duration, handling division by zero. Validate results with sample data and consider edge cases like streamers with no streams in the category.

5. Optimize and Discuss Scalability

Suggest indexing on streamer_id and category, and mention that for large datasets, pre-aggregation or materialized views may be beneficial. Discuss trade-offs between readability and performance.

Key Points to Mention

  • Use of GROUP BY and aggregate functions like SUM.
  • Conditional aggregation with CASE WHEN or FILTER (in PostgreSQL).
  • Case sensitivity handling: LOWER()/UPPER() or COLLATE.
  • Parameterization to prevent SQL injection and improve reusability.
  • Handling NULLs and division by zero in ratio calculation.
  • Performance considerations: indexing and query optimization.

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

Q3

Identify streamers who streamed more in a given month than they did the previous month. How do you handle year boundaries and months with no data (NULL values)?

Product Analytics & MetricsAlgorithms & Data Structures
Author's notes

This is where LAG() comes in and I knew that much.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the data schema and definitions (e.g., what constitutes a stream, how months are defined). Then, outline a SQL-based solution using a self-join or window function to compare each streamer's monthly stream count with the previous month, explicitly handling year boundaries and NULLs. Finally, discuss edge cases and potential optimizations.

Pro tip: Mention that you would validate the results by spot-checking streamers with known activity and ensure that the comparison is done on a per-streamer basis, not globally. Also, consider using a calendar table to handle missing months.

1. Clarify requirements and data

Ask about the data schema, definition of a stream, and how months are represented. Confirm whether the comparison is month-over-month for each streamer and how to handle missing months.

2. Aggregate monthly streams

Write a query to count streams per streamer per month, ensuring that months with no streams are included with a count of 0 (e.g., using a calendar table or generating a series).

3. Compare with previous month

Use a window function like LAG to get the previous month's stream count for each streamer, handling year boundaries by ordering by year and month correctly.

4. Filter and handle NULLs

Filter for streamers where current month's streams > previous month's streams. For NULLs (e.g., first month or missing data), decide whether to treat as 0 or exclude, and document the assumption.

5. Validate and discuss edge cases

Mention validation steps (e.g., spot-checking) and discuss edge cases like streamers with no data in the previous month, leap years, and timezone considerations.

Key Points to Mention

  • Use of window functions (LAG) or self-join to compare consecutive months
  • Handling year boundaries by ordering on year and month (e.g., YYYY-MM format)
  • Treatment of NULL values: either fill with 0 using COALESCE or exclude, with justification
  • Importance of a calendar table or date spine to include months with no data
  • Partitioning by streamer ID to ensure per-streamer comparison
  • Validation and edge cases: timezones, leap years, and data completeness

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

Q4

Review this SQL query that calculates average concurrent viewers per streamer in 2019 and total view time from US viewers. Is it correct, and if not, what's wrong with it?

Product Analytics & MetricsRoot Cause Analysis
Author's notes

The query joins minute_streamed and minute_viewed on streamer_username without a time join condition, so it creates a many-to-many mess.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, restate the query's intent to confirm understanding, then systematically verify each clause against the requirements. Check for common SQL pitfalls like incorrect aggregation, missing filters, and join errors, and propose corrections with explanations.

Pro tip: Always clarify ambiguous terms like 'average concurrent viewers' and 'total view time' before diving into code review, as these definitions drive the correct SQL logic. Mention that you'd validate the query with sample data or edge cases to ensure accuracy.

1. Clarify Requirements

Restate the goal: calculate average concurrent viewers per streamer in 2019 and total view time from US viewers. Confirm definitions of 'concurrent viewers', 'view time', and 'US viewers'.

2. Review Query Structure

Examine the SELECT, FROM, WHERE, GROUP BY, and JOIN clauses to ensure they align with the requirements. Check for correct aggregation and filtering.

3. Identify Common Errors

Look for issues like missing date filters, incorrect join conditions, wrong aggregation functions, or improper handling of time zones and session overlaps.

4. Propose Corrections

Suggest specific fixes, such as adding WHERE clauses for year and country, using appropriate window functions for concurrency, and ensuring correct grouping.

5. Validate and Test

Recommend testing the corrected query with sample data or edge cases to ensure it produces accurate results.

Key Points to Mention

  • Date filtering: Ensure the query filters for the year 2019 using appropriate date functions.
  • Country filter: Verify that the query correctly filters for US viewers, considering potential country code mappings.
  • Concurrency calculation: Average concurrent viewers requires handling overlapping sessions, possibly using window functions or self-joins.
  • View time aggregation: Total view time should sum session durations, not just count sessions.
  • Join correctness: Check that joins between streamers, streams, and viewers are on the correct keys and don't introduce duplicates.
  • Grouping: Ensure GROUP BY includes streamer ID and any other non-aggregated columns.

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