← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Uber coding round, algorithm-heavy. One problem but it had two parts and the monotonic stack angle wasn't immediately obvious to me.

Questions Asked (1)

Q1

Given an array of item prices and a discount rule where each item gets a discount equal to the price of the next cheaper-or-equal item that appears after it in the array, compute the total amount paid after discounts and identify which items received no discount at all. Solve it in O(n).

Algorithms & Data Structures
Author's notes

Two-parter which I didn't see coming.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a monotonic stack to efficiently find the next smaller or equal element for each item, enabling O(n) time. Compute the total discount by summing these next smaller-or-equal values, then subtract from the total price to get the amount paid. Items with no such element receive no discount and should be identified.

Pro tip: Clarify whether the discount is applied per item or cumulatively, and confirm that 'next cheaper-or-equal' means the nearest subsequent element with value <= current. This shows attention to detail and avoids misinterpretation.

1. Clarify the problem

Restate the discount rule and confirm edge cases: equal prices count, no discount if no such element, and discounts are per item. Ask about input constraints (e.g., array size, price range).

2. Design the algorithm

Use a monotonic stack to find the next smaller-or-equal element for each item in O(n). Iterate through the array, maintaining a stack of indices with non-decreasing prices; when a smaller-or-equal price is found, pop and record the discount.

3. Compute total and identify no-discount items

While processing, accumulate the total discount and track items that never get a discount (i.e., remain in the stack at the end). Alternatively, after computing discounts, sum them and collect indices with zero discount.

4. Verify with examples

Walk through a small example (e.g., [10, 5, 8, 3]) to ensure the algorithm correctly computes discounts and identifies no-discount items. Check edge cases like strictly increasing or decreasing arrays.

5. Analyze complexity

Explain that each element is pushed and popped at most once, giving O(n) time and O(n) space for the stack. Discuss potential optimizations if space is a concern.

Key Points to Mention

  • Monotonic stack technique for next smaller-or-equal element
  • Time complexity O(n) and space complexity O(n)
  • Handling of equal prices (<= condition)
  • Identification of items with no discount (those without a next smaller-or-equal)
  • Edge cases: empty array, single element, all equal prices
  • Potential alternative approaches (e.g., brute force O(n^2)) and why they are inefficient

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.