Final Price Discount
Updated
Problem statement
Given an array of product prices prices, compute the final price of every product.
For the product at index i, find the smallest index j such that j > i and prices[j] <= prices[i]. If such an index exists, use prices[j] as the discount and set the final price to prices[i] - prices[j]. If no such index exists, keep prices[i] unchanged.
Return the final prices in the original item order.
Function
finalPrices(prices: int[]) → int[]Examples
Example 1
prices = [8,4,6,2,3]return = [4,2,4,2,3]The first eligible prices to the right are 4 for price 8, 2 for price 4, and 2 for price 6. The last two products have no eligible price to their right.
Example 2
prices = [5,5,3,6]return = [0,2,3,6]The second price 5 is the first eligible discount for the first product, so equality is allowed and its final price is 0. The next product uses price 3 as its discount. Prices 3 and 6 remain unchanged.
Constraints
1 <= prices.length <= 1000001 <= prices[i] <= 1000000000
More Microsoft Online Assessment questions
- Maximum Score with Prime JumpsOA · Seen Dec 2026
- Minimum Non-Adjacent Selection CapabilityOA · Seen Dec 2026
- Authentication SystemOA · Seen Nov 2026
- Maximum String OperationsOA · Seen Nov 2026
- Minimum Cost to Process Priority TasksOA · Seen Nov 2026
- Most Visited Sprint MarkerOA · Seen Nov 2026
- Maximum Escape Game ScoreOA · Seen Nov 2026
- Count Subarrays with a Perfect-Square ProductOA · Seen Nov 2026
- Maximum Interactive Team SizeOA · Seen Nov 2026
- Maximize Pipeline Throughput Under a Scaling BudgetOA · Seen Nov 2026
Similar questions at other companies
- Compare Character Arrays with BackspacesOdoo · OAOdoo interview questions
- Maximize Shipment Order by Sorting One WindowAmazon · OA
- Most Segments That Sort the ArrayRippling · OARippling interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Sum of Subarray MinimumsNVIDIA · OANVIDIA interview questions
- Maximum Sum of HeightsVirtu Financial · OAVirtu Financial interview questions
- Nested Recipe Blocks (Game Adaptation)Roblox · OARoblox interview questions
- Maximum Capped Histogram Area After One RemovalMyntra · OAMyntra interview questions
- Trapping Rain WaterGoogle · OAGoogle interview questions
- Shopkeeper Final PriceUber · OAUber interview questions
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)