Maximize Distinct Purchases Within Budget Constraints
Company: TikTok
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
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.
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
- Sort (price, index) pairs by price ascending, breaking ties by index.
- Iterate the sorted list and keep a running total; add an item if it keeps the total within budget.
- Track original indices while sorting; return chosen indices sorted ascending.