← TikTok Interview Insights

TikTok·Data Scientist·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

SQL-heavy technical screen for a Data Scientist role at TikTok. The main problem was a product catalog question requiring both a window function version and a subquery version, plus a discussion of NULL handling and numeric precision. Felt like a mid-level data engineering question more than a pure DS one, which threw me off a bit.

Questions Asked (2)

Q1

Given a products table with list_price and a nullable sale_price, write a SQL query that returns exactly one product per category: the one with the highest absolute discount (defined as max(list_price minus COALESCE(sale_price, list_price), 0)), breaking ties by smallest product_id. Return category, product_id, list_price, sale_price, and discount_amount.

Data ModelingTechnical Trade-offsAlgorithms & Data Structures
Author's notes

This is the core question and it has a few layers.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the discount definition and edge cases, then outline a window-function solution using ROW_NUMBER() partitioned by category and ordered by discount DESC, product_id ASC. Walk through the query step by step, explaining how COALESCE handles nulls and why the tie-breaker ensures exactly one row per category.

Pro tip: Mention that you would test with edge cases like all-null sale_price or ties, and note that using a subquery or CTE improves readability and avoids repeating the discount expression.

1. Clarify requirements and edge cases

Confirm the discount formula, null handling, and tie-breaking rule. Ask about expected output format and whether categories with no products should appear.

2. Design the query structure

Use a CTE or subquery to compute discount_amount for each product, then apply a window function to rank products within each category.

3. Implement ranking and filtering

Use ROW_NUMBER() OVER (PARTITION BY category ORDER BY discount_amount DESC, product_id ASC) and filter for rank = 1 to get exactly one product per category.

4. Select final columns and validate

Return category, product_id, list_price, sale_price, and discount_amount. Mentally test with sample data including nulls and ties to ensure correctness.

Key Points to Mention

  • Use COALESCE(sale_price, list_price) to treat null sale_price as no discount.
  • Compute discount as GREATEST(list_price - COALESCE(sale_price, list_price), 0) to avoid negative discounts.
  • Apply ROW_NUMBER() with PARTITION BY category and ORDER BY discount DESC, product_id ASC for deterministic tie-breaking.
  • Filter using a CTE or subquery to select only rank = 1 rows.
  • Consider performance implications: window functions may require sorting, but are efficient for this task.
  • Test edge cases: all null sale_price, ties in discount, categories with single product.

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

Q2

Walk through how your query handles NULL sale_price values and avoids floating point precision issues with the discount calculation.

Technical Trade-offsData Modeling
Author's notes

They asked this as a follow-up after I wrote both versions.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining how you handle NULL sale_price values, such as using COALESCE or filtering them out, and then describe how you avoid floating point precision issues by using decimal types or integer arithmetic. Emphasize the trade-offs and why your approach is robust for production.

Pro tip: Mention that you validate the discount calculation with unit tests and consider edge cases like NULLs and rounding errors, showing you think about data quality and reliability.

1. Handle NULL sale_price

Explain how you treat NULL sale_price values, e.g., by excluding them from the calculation or replacing them with a default value using COALESCE, and justify your choice based on business logic.

2. Avoid floating point precision issues

Describe using fixed-point decimal types (e.g., DECIMAL in SQL) or integer arithmetic (e.g., storing prices in cents) to prevent rounding errors in discount calculations.

3. Implement the discount calculation

Show how you compute the discount, ensuring that the method aligns with the chosen precision strategy, and mention any rounding rules (e.g., ROUND to 2 decimal places).

4. Validate and test

Discuss how you test the query with edge cases, including NULLs and values that could cause precision issues, to ensure correctness.

5. Consider performance and scalability

Briefly mention any performance implications of your approach, such as indexing or avoiding functions on columns, especially for large datasets.

Key Points to Mention

  • Use of COALESCE or NULLIF to handle NULL sale_price
  • Use of DECIMAL data type or integer cents to avoid floating point errors
  • Explicit rounding rules (e.g., ROUND function) to ensure consistent results
  • Testing with edge cases like NULLs and boundary values
  • Trade-offs between precision and performance
  • Documentation of assumptions and business rules for NULL handling

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