PracHub
QuestionsPremiumLearningGuidesCheatsheetNEW

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

Solution

from typing import List

def maximize_purchases(prices: List[int], budget: int) -> List[int]:
    indexed = [(p, i) for i, p in enumerate(prices)]
    # Sort by price, then by index to ensure deterministic tie-breaking
    indexed.sort(key=lambda x: (x[0], x[1]))

    total = 0
    chosen = []  # store original indices
    for price, idx in indexed:
        if total + price <= budget:
            total += price
            chosen.append(idx)
        # else skip; later items are not cheaper due to sorting

    # Return indices in increasing order as required
    chosen.sort()
    return chosen
Explanation
Sorting items by (price, index) ensures that greedily taking items in that order yields the maximum count within budget. This also minimizes total cost for that count because the chosen set is the set of the cheapest possible items. Ties on equal prices are broken by index to ensure lexicographically smallest indices among equal-cost optimal solutions. Finally, the result is returned as indices in increasing order.

Time complexity: O(n log n). Space complexity: O(n).

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 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

  • 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)