Reported September 2026
Microsoftmonotonic stack

Shopkeeper Final Price Summary

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

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

The Shopkeeper Final Price Summary problem showed up in a Microsoft OA reported in September 2026, and the detail that trips people is the second output line: indices of items sold at full price, space-separated, in a String[]. The core is a classic next-smaller-or-equal lookup, so it's a monotonic stack problem. If you've got the OA in a day or two, learn the stack loop and the output formatting. StealthCoder is there as a safety net during the live assessment if you blank, but this one is very learnable.

The problem

A shopkeeper arranges items in a list for a sale. For each item, find the first item to its right whose price is less than or equal to the current price.
If such an item exists, subtract its price from the current item's price. Otherwise, the current item is sold at full price.
Return a two-element String[] representing the required output lines:
The first string is the sum of the final costs of all items.
The second string contains the 0-based indices of every item sold at full price, separated by single spaces and listed in ascending order.

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

Examples
Example 1
prices = [2,3,1,2,4,2]
return = ["8","2 5"]
The final costs are [1,2,1,0,2,2], whose sum is 8. Items at indices 2 and 5 have no equal-or-lower-priced item to their right, so they are sold at full price.
Example 2
prices = [1,2,3,4]
return = ["10","0 1 2 3"]
No item has an equal-or-lower-priced item to its right. Every item is sold at full price, the total is 10, and all indices appear on the second output line.

Constraints
1 <= prices.length <= 10^5
1 <= prices[i] <= 10^6

Reported by candidates. Source: FastPrep

Pattern and pitfall

The brute force scans right for every item and hits O(n^2). With n up to 10^5, that's too slow. The trick is a monotonic stack. Walk the array left to right. While the stack top has a price greater than or equal to the current price, pop it, subtract the current price from it, and record its final cost. Then push the current index. Anything left on the stack at the end has no equal-or-lower item to its right, so it's sold at full price. Collect those indices, sort them ascending (stack order already is), and join with spaces. The pitfall is using strict less-than instead of less-than-or-equal. Example 1 has equal 2s, so that matters. Also sum into a long, since 10^5 times 10^6 overflows an int. If you freeze on the stack direction during the OA, StealthCoder can hand you the working loop as a hedge.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Shopkeeper Final Price Summary 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

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 Microsoft's OA.

Microsoft reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Shopkeeper Final Price Summary FAQ

What's the trick for Shopkeeper Final Price Summary?+

Use a monotonic stack of indices. Scan left to right and pop every index whose price is greater than or equal to the current price, applying the discount as you pop. Whatever remains on the stack at the end is sold at full price. One pass, O(n).

How hard is this Microsoft OA question really?+

Easy to medium. The logic is a standard next-smaller-element pattern with one twist, the less-than-or-equal comparison. The harder part is the output format: a two-element String[] with the total and a space-joined index list. Most people lose points on small details, not the idea.

Will brute force pass with n up to 10^5?+

No. Scanning right from every item is O(n^2), which is around 10^10 operations in the worst case, for example a strictly increasing or constant array. The stack solution runs in linear time and is what the constraints are pushing you toward.

What edge cases should I test?+

Test a single item, which is sold at full price so the output is its price and index 0. Test a strictly increasing array, where all indices are full price, like Example 2. Test equal prices, where the discount equals the price and the cost becomes 0. Also test large sums for overflow.

How do I prepare for this in 48 hours?+

Write the next-smaller-or-equal stack loop from memory twice. Then practice building the output: sum as a long, the index string joined by single spaces. Run both examples by hand. If the full price list is empty, think about what the second string should be, though the constraints make this unlikely.

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

OA at Microsoft?
Invisible during screen share
Get it