← Questions

Final prices after next-lower-price discounts

0
UberCodingLast asked Feb 2026

A coding interview question asked in Uber's online assessment, last asked Feb 2026. It tests monotonic stack and arrays.

You get the prices of items in a shop, in order. Each item is discounted by the price of the first later item that costs the same or less. Item i therefore sells for prices[i] - prices[j], where j is the smallest index after i with prices[j] <= prices[i]. An item with no such later item sells at full price. Return every item's final price.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • final_prices(prices: list[int]) -> list[int].

Example

final_prices([8, 4, 6, 2, 3])  ->  [4, 2, 4, 2, 3]
# 8 is discounted by 4, 4 by 2, 6 by 2; nothing after 2 or 3 is cheaper or equal
final_prices([10, 1, 1, 6])    ->  [9, 0, 1, 6]     # an equal price counts
final_prices([1])              ->  [1]

Constraints

  • 1 <= len(prices) <= 2 × 10^5, 1 <= prices[i] <= 10^9.

Clarifying questions

  • Does an equal later price count as a discount, or only a strictly lower one?
  • Is the discount the first such price to the right, or the lowest one?
  • Should I return the final prices, their total, or something else?

What a strong answer covers

  • The brute force: for each item scan right for the first cheaper-or-equal price, O(n²), too slow at this size.
  • A monotonic stack of indices still waiting for a discount; when the current price is at most the price on top, pop it and apply the discount. Each index is pushed and popped once: O(n) time, O(n) space.
  • Why the stack stays strictly increasing in price from bottom to top.
  • Tests: strictly increasing prices (no discounts), strictly decreasing, all equal.

Follow-ups

  • Return the total of all final prices and the sorted list of indices sold at full price.
  • Only a strictly lower price counts. What changes in the stack condition?
  • The discount comes from the first cheaper-or-equal item within the next k items only.

Where it's asked

  • Uber · Online assessment · Last asked Feb 202690 min · Two problems on a HackerRank-based platform, camera and screen share on · No AI

What it tests

monotonic stack · arrays

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding