Apply the Next Cheaper Item Discount Across a Marketplace Cart
Last updated: November 29, 2026
Quick Overview
Each cart item gets a discount equal to the price of the first later item whose price is less than or equal to its own; return the final price of every item.
Meta
November 29, 20260
0
0 solved
Each cart item gets a discount equal to the price of the first later item whose price is less than or equal to its own; return the final price of every item.
An easy Meta phone-screen warm-up framed as a Facebook Marketplace promotion rule. The interviewer expects the O(n^2) scan quickly, then the monotonic stack version, and checks that you get the less-than-or-equal tie rule right.
What the Interviewer Expects
- Restate the rule precisely, including ties
- Give the simple nested-loop solution
- Improve to O(n) with an increasing stack of indices
- Return a new list without mutating input unless allowed
- Test with ties and a no-discount case
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Only discount if the later item is strictly cheaper.
- Apply the discount from the nearest cheaper item on either side.
- Return the total savings across the cart.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list prices of non-negative integers in cart order. Output: list final where final[i] = prices[i] - prices[j] for the smallest j > i with prices[j] <= prices[i], or prices[i] if the...
High-Level Design
This is next-smaller-or-equal. Keep a stack of indices still waiting for a discount; their prices increase strictly from bottom to top. When a new price is less than or equal to the top's price, the n...