PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates array-processing and algorithmic problem-solving skills, focusing on reasoning about subsequent elements to compute element-wise discounts under input constraints.

  • medium
  • Uber
  • Coding & Algorithms
  • Software Engineer

Compute final prices with next smaller discount

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

You are given an integer array `prices` of length `n`, where `prices[i]` is the original price of the `i`-th item. For each item `i`, find the first index `j > i` such that `prices[j] <= prices[i]`. That `prices[j]` becomes the discount for item `i`, so the final price of item `i` is: - `prices[i] - prices[j]` if such `j` exists - otherwise `prices[i]` Return an array `finalPrices` of length `n` containing the final price for each item. #### Input - `prices`: array of integers #### Output - `finalPrices`: array of integers #### Example - Input: `prices = [8, 4, 6, 2, 3]` - Output: `[4, 2, 4, 2, 3]` #### Constraints (typical) - `1 <= n <= 2e5` - `1 <= prices[i] <= 1e9`

Quick Answer: This question evaluates array-processing and algorithmic problem-solving skills, focusing on reasoning about subsequent elements to compute element-wise discounts under input constraints.

You are given an integer array prices where prices[i] is the original price of the i-th item. For each item i, find the first index j > i such that prices[j] <= prices[i]. If such an index exists, prices[j] is used as the discount for item i, and the final price is prices[i] - prices[j]. If no such index exists, the final price remains prices[i]. Return an array finalPrices containing the final price of each item.

Constraints

  • 1 <= len(prices) <= 200000
  • 1 <= prices[i] <= 1000000000

Examples

Input: ([8, 4, 6, 2, 3],)

Expected Output: [4, 2, 4, 2, 3]

Explanation: Item 0 gets discount 4, item 1 gets discount 2, item 2 gets discount 2, and the last two items have no later smaller-or-equal discount.

Input: ([1],)

Expected Output: [1]

Explanation: A single item has no later item to provide a discount.

Input: ([1, 2, 3, 4],)

Expected Output: [1, 2, 3, 4]

Explanation: Prices are strictly increasing, so no item has a later price less than or equal to itself.

Input: ([10, 7, 5, 3],)

Expected Output: [3, 2, 2, 3]

Explanation: Each item except the last receives the immediately following item's price as its discount.

Input: ([5, 5, 5],)

Expected Output: [0, 0, 5]

Explanation: Equal prices qualify as discounts. The first two items each use the next equal price as a discount.

Input: ([1000000000, 1, 1000000000],)

Expected Output: [999999999, 1, 1000000000]

Explanation: The first item is discounted by 1. The second and third items have no later smaller-or-equal price.

Solution

def solution(prices):
    final_prices = prices[:]
    stack = []

    for i, price in enumerate(prices):
        while stack and prices[stack[-1]] >= price:
            idx = stack.pop()
            final_prices[idx] -= price
        stack.append(i)

    return final_prices

Time complexity: O(n). Space complexity: O(n).

Hints

  1. A brute-force search for each item would be too slow for large inputs. Think about how to remember items that are still waiting for a discount.
  2. Use a monotonic stack of indices. When the current price is less than or equal to the price at the top index, it can serve as that item's discount.
Last updated: Jun 21, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Maximize Throughput and Count Trigger Components - Uber (medium)
  • Replace Dashes With Nearest Letters - Uber (medium)
  • Find Earliest Column With One - Uber (easy)
  • Solve Wonderful Strings and Grid Queries - Uber (hard)
  • Count Islands After Land Additions - Uber (medium)