Reported June 2026
Ubermonotonic stack

Shopkeeper Final Price

Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Uber OA. Under 2s to a working solution.
Founder's read

The edge case that kills the naive answer here is equal prices. In this Uber OA, reported in June 2026, the discount comes from the next item to the right that is equal or lower, and that "or equal" is where people slip. It's a next-smaller-or-equal problem on up to 100,000 items, so the O(n^2) scan times out. The pattern is a monotonic stack. If you blank on the stack direction during the live assessment, StealthCoder is the safety net that reads the problem and hands you the solution while staying invisible on screen share.

The problem

A shopkeeper arranges items in a list for a sale. Starting from the left, each item is sold at its full price minus the price of the first item to its right that is of equal or lower price.
If no such item exists, the current item is sold at its full price.
Print the sum of the final cost for all items, then on the next line print a space-separated, 0-based list of the indices of items that are sold at full price, in ascending order.
Complete the function finalPrice in the editor with the following parameter:
int prices[n]: an array of item prices
Return a two-element String[] representing the required output lines: the total cost as the first string, and the space-separated full-price item indices as the second string.
1 <= n <= 10^5
1 <= prices[j] <= 10^9, where 0 <= j < n

Function
finalPrice(prices: int[]) → String[]

Examples
Example 1
prices = [9, 2, 1, 2, 4, 2]
return = ["13","2 5"]
For item prices [9, 2, 1, 2, 4, 2]:
Item 0, priced at 9, is discounted by the next item priced 2, so its final cost is 7.
Item 1, priced at 2, is discounted by the next equal-or-lower item priced 1, so its final cost is 1.
Item 2, priced at 1, sells at full price because there is no equal-or-lower item to its right.
Item 3, priced at 2, is discounted by the later item priced 2, so its final cost is 0.
Item 4, priced at 4, is discounted by the later item priced 2, so its final cost is 2.
Item 5, priced at 2, sells at full price.
The total cost is 7 + 1 + 1 + 0 + 2 + 2 = 13, and the full-price item indices are 2 5.
The output lines are 13 and 2 5.
Example 2
prices = [5, 1, 3, 4, 6, 2]
return = ["14","1 5"]
For item prices [5, 1, 3, 4, 6, 2]:
Item 0, priced at 5, is discounted by the next item priced 1, so its final cost is 4.
Item 1, priced at 1, sells at full price because there is no equal-or-lower item to its right.
Item 2, priced at 3, is discounted by the later item priced 2, so its final cost is 1.
Item 3, priced at 4, is discounted by the later item priced 2, so its final cost is 2.
Item 4, priced at 6, is discounted by the later item priced 2, so its final cost is 4.
Item 5, priced at 2, sells at full price.
The total cost is 4 + 1 + 1 + 2 + 4 + 2 = 14, and the full-price item indices are 1 5.
The output lines are 14 and 1 5.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Walk the array from left to right with a stack of indices whose prices haven't found a discount yet. For each new price, while the stack top has a price greater than or equal to the current one, pop it, subtract the current price from it, and record that final cost. After the loop, anything left on the stack sells at full price. Sort those indices ascending, or collect them in stack order, which is already ascending. The pitfall is using strict greater-than, which misses the equal case. In Example 1, the 2 at index 3 gets discounted by the 2 at index 5, so equal must pop. Also sum in a 64-bit type, since 10^5 items at 10^9 overflow 32 bits. And format the output as two strings, even when the index list is empty. If the stack logic slips under pressure, StealthCoder is the hedge that gives you the working code.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Shopkeeper Final Price cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as final prices with a special discount in a shop. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Uber's OA.

Uber reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Shopkeeper Final Price FAQ

What's the trick in Shopkeeper Final Price?+

Use a monotonic stack to find the next item to the right with equal or lower price. Pop while the top is greater than or equal to the current price, apply the discount, then push the current index. Whatever's left at the end sold at full price. One pass, O(n).

Why does the brute force fail?+

Scanning right from every item is O(n^2). With n up to 10^5, that's around 10^10 operations in the worst case, such as a strictly increasing array. The stack makes it linear because each index is pushed and popped at most once.

What's the most common bug?+

Using strict comparison. The rule says equal or lower, so an item with the same price as the current one must be popped and discounted. In Example 1, index 3 (price 2) is discounted by index 5 (price 2) and costs 0.

Do I need to worry about overflow?+

Yes. Prices go up to 10^9 and n up to 10^5, so the total can reach 10^14. Use a long or 64-bit integer for the sum. In Python it's not an issue, but in Java or C++ an int will give wrong answers on big tests.

How do I prepare for this in 48 hours?+

Practice next-smaller-element with a stack until you can write it without thinking. Then tweak it for the equal case and for tracking which indices never get popped. Also practice returning the output as two strings, joined by spaces, since the format is easy to get wrong.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Uber.

OA at Uber?
Invisible during screen share
Get it