← Questions

Final discounted prices and the items sold at full price

0
MicrosoftCodingLast asked Mar 2026

A coding interview question asked in Microsoft's technical phone screen, last asked Mar 2026. It tests stacks and arrays.

Items are sold from left to right. Item i gets a discount equal to the price of the first later item j > i with prices[j] <= prices[i], so it sells for prices[i] - prices[j]. If no such later item exists, it sells at full price. Return the total of all final prices and the indices of the items sold at full price.

Interface

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

  • final_prices(prices: list[int]) -> tuple[int, list[int]] — (total, full_price_indices), with indices 0-based and increasing.

Example

final_prices([5, 1, 3])        ->  (8, [1, 2])    # 5-1=4, then 1 and 3 at full price
final_prices([8, 4, 6, 2, 3])  ->  (15, [3, 4])   # final prices [4, 2, 4, 2, 3]
final_prices([3, 3, 3])        ->  (3, [2])       # an equal price counts: 0 + 0 + 3

Constraints

  • 0 <= len(prices) <= 2 * 10^5
  • 0 <= prices[i] <= 10^9; the total needs a 64-bit integer.

Clarifying questions

  • Does an equal later price count as a discount (<=), or only a strictly lower one?
  • Is the discount the first qualifying item, or the smallest one to the right?
  • What should be returned for an empty list?

What a strong answer covers

  • The O(n^2) scan as a baseline.
  • A monotonic stack of indices still waiting for a discount: each new price pops every waiting item whose price is greater than or equal to it and becomes their discount.
  • Items left on the stack at the end are the full-price ones, already in increasing index order.
  • O(n) time (each index is pushed and popped once) and O(n) space; 64-bit totals.
  • Tests: empty, strictly increasing (every item at full price), strictly decreasing (only the last at full price), equal prices.

Follow-ups

  • The discount is the first strictly lower price instead.
  • Items are sold in a circle, so the search wraps around once.
  • Return each item's final price as well, streaming prices in one at a time.

Where it's asked

What it tests

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