PracHub
QuestionsLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of selection and optimization under budget constraints, algorithmic efficiency, and the ability to implement correct logic for maximizing the number of distinct items purchased.

  • medium
  • TikTok
  • Coding & Algorithms
  • Data Scientist

Maximize Distinct Purchases Within Budget Constraints

Company: TikTok

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

##### Scenario Given a customer budget and a list of product prices, determine the maximum number of distinct products the customer can afford. ##### Question Design an algorithm that lists the products a customer can purchase within their budget while maximizing the count of items bought. ##### Hints Sort prices ascending, add items greedily until the running total would exceed the budget.

Quick Answer: This question evaluates understanding of selection and optimization under budget constraints, algorithmic efficiency, and the ability to implement correct logic for maximizing the number of distinct items purchased.

Given a non-negative integer budget and an array prices where prices[i] is the price of the i-th product, choose a subset of product indices such that the total cost does not exceed budget and the number of chosen products is maximized. If multiple subsets achieve the same maximum count, prefer the one with the smallest total cost; if still tied, return the lexicographically smallest list of indices. Each index represents a distinct product even if prices repeat. Return the chosen indices in increasing order.

Constraints

  • 0 <= n <= 200000 where n = len(prices)
  • 0 <= prices[i] <= 10^9
  • 0 <= budget <= 10^14
  • Indices are 0-based
  • Return indices in increasing order

Hints

  1. Sort (price, index) pairs by price ascending, breaking ties by index.
  2. Iterate the sorted list and keep a running total; add an item if it keeps the total within budget.
  3. Track original indices while sorting; return chosen indices sorted ascending.
Last updated: Mar 29, 2026

Loading coding console...

PracHub

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

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding 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

  • 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)
  • Solve common string/DP/stack problems - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)