PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/TikTok

Maximize products bought under budget

Last updated: Mar 29, 2026

Quick Overview

This question evaluates algorithmic problem-solving skills including sorting, prefix-sum accumulation, binary search, greedy selection, careful tie-breaking for lexicographic order, and complexity analysis.

  • Medium
  • TikTok
  • Coding & Algorithms
  • Data Scientist

Maximize products bought under budget

Company: TikTok

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Take-home Project

Given N products and M customers, for each customer find the list of distinct products they can buy without exceeding their budget such that the number of products purchased is maximized. If multiple solutions yield the same maximum count: (1) choose the set with the smallest total spend; (2) if still tied, choose the lexicographically smallest list of product_ids. You may use each product at most once per customer; assume infinite stock across customers (each customer considers the full catalog independently). Return, for each customer, (customer_id, max_count, chosen_product_ids in ascending order by price then product_id). Design an algorithm with preprocessing O(N log N) and query time O(log N) per customer (excluding output size), and implement it. Justify correctness. Input format - products: list of (product_id INT, price INT) with price >= 0 - customers: list of (customer_id INT, budget INT) with budget >= 0 Constraints - 1 ≤ N, M ≤ 200,000; 0 ≤ price, budget ≤ 1e9 - Ties on price are broken by smaller product_id Required approach - Pre-sort products by (price ASC, product_id ASC) and build prefix sums of prices and parallel array of product_ids. - For each budget B, find k = max index such that prefix_sum[k] ≤ B via binary search; output the first k product_ids in that sorted order. Example products = [(1,100),(2,100),(3,300),(4,500)] customers = [("A",500)] Output: ("A", 3, [1,2,3]) because taking the three cheapest maximizes count; total spend 500. Edge cases to handle - Budget < cheapest price → empty list. - Budget ≥ sum of all prices → all product_ids. - Many equal prices: verify lexicographic tie-break by product_id. Deliverables - Working implementation (e.g., Python) and time/space complexity analysis.

Quick Answer: This question evaluates algorithmic problem-solving skills including sorting, prefix-sum accumulation, binary search, greedy selection, careful tie-breaking for lexicographic order, and complexity analysis.

Related Interview Questions

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Maximize sum with no adjacent elements - TikTok (medium)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)
TikTok logo
TikTok
Oct 13, 2025, 9:49 PM
Data Scientist
Take-home Project
Coding & Algorithms
2
0

Given N products and M customers, for each customer find the list of distinct products they can buy without exceeding their budget such that the number of products purchased is maximized. If multiple solutions yield the same maximum count: (1) choose the set with the smallest total spend; (2) if still tied, choose the lexicographically smallest list of product_ids. You may use each product at most once per customer; assume infinite stock across customers (each customer considers the full catalog independently). Return, for each customer, (customer_id, max_count, chosen_product_ids in ascending order by price then product_id). Design an algorithm with preprocessing O(N log N) and query time O(log N) per customer (excluding output size), and implement it. Justify correctness.

Input format

  • products: list of (product_id INT, price INT) with price >= 0
  • customers: list of (customer_id INT, budget INT) with budget >= 0

Constraints

  • 1 ≤ N, M ≤ 200,000; 0 ≤ price, budget ≤ 1e9
  • Ties on price are broken by smaller product_id

Required approach

  • Pre-sort products by (price ASC, product_id ASC) and build prefix sums of prices and parallel array of product_ids.
  • For each budget B, find k = max index such that prefix_sum[k] ≤ B via binary search; output the first k product_ids in that sorted order.

Example products = [(1,100),(2,100),(3,300),(4,500)] customers = [("A",500)] Output: ("A", 3, [1,2,3]) because taking the three cheapest maximizes count; total spend 500.

Edge cases to handle

  • Budget < cheapest price → empty list.
  • Budget ≥ sum of all prices → all product_ids.
  • Many equal prices: verify lexicographic tie-break by product_id.

Deliverables

  • Working implementation (e.g., Python) and time/space complexity analysis.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More TikTok•More Data Scientist•TikTok Data Scientist•TikTok Coding & Algorithms•Data Scientist Coding & Algorithms
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.