PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates proficiency in data manipulation and SQL querying—specifically use of window functions versus set-based alternatives, group-level ranking and tie-breaking, NULL-aware expressions, and numeric precision when computing discounts within the Data Manipulation (SQL/Python) domain.

  • Medium
  • TikTok
  • Data Manipulation (SQL/Python)
  • Data Scientist

Select max-discount product per category

Company: TikTok

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: Medium

Interview Round: Take-home Project

You have a catalog of products. For each category, return exactly one product: the one with the largest absolute discount; if multiple products in the same category have the same discount, return the one with the smallest product_id. Define absolute discount as max(list_price - COALESCE(sale_price, list_price), 0). Return: category, product_id, list_price, sale_price, discount_amount. Write a single SQL query that works if sale_price can be NULL and never below 0, using window functions; also show a version without window functions. Schema and sample data: Tables - products(product_id INT PRIMARY KEY, category VARCHAR(50) NOT NULL, list_price DECIMAL(10,2) NOT NULL, sale_price DECIMAL(10,2) NULL) Sample rows (products) product_id | category | list_price | sale_price 101 | Phones | 799.00 | 699.00 102 | Phones | 999.00 | 749.00 103 | Phones | 999.00 | 749.00 201 | Laptops | 1299.00 | 1099.00 202 | Laptops | 1399.00 | 1199.00 301 | TVs | 899.00 | NULL Expected result rows category | product_id | list_price | sale_price | discount_amount Phones | 102 | 999.00 | 749.00 | 250.00 Laptops | 201 | 1299.00 | 1099.00 | 200.00 TVs | 301 | 899.00 | NULL | 0.00 Sub-questions: - Provide the window-function solution (ROW_NUMBER partitioned by category ordered by discount_amount DESC, product_id ASC). - Provide a without-window solution (e.g., join to a per-category max-discount subquery with tie-break on product_id). - Explain how your query avoids floating point precision issues and handles NULL sale_price.

Quick Answer: This question evaluates proficiency in data manipulation and SQL querying—specifically use of window functions versus set-based alternatives, group-level ranking and tie-breaking, NULL-aware expressions, and numeric precision when computing discounts within the Data Manipulation (SQL/Python) domain.

Last updated: Mar 29, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find high-value crypto users and top-CTR product - TikTok (easy)
  • Write monthly customer and sales SQL queries - TikTok (easy)
  • Find top-paid employee per department - TikTok (easy)
  • Count buggy vs non-buggy by employer - TikTok (Medium)
  • Write SQL for 7-day geo-localized revenue dashboard - TikTok (Medium)