← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jul 2026

Summary

Uber SWE interview with a monotonic stack problem that looks straightforward but has a sneaky second part most people probably miss on first read.

Questions Asked (1)

Q1

Given a prices array, apply a discount rule where each item's discount equals the price of the next cheaper-or-equal item that comes after it. Return both the sum of all final prices and the list of indices where no discount was applied, using O(n) time.

Algorithms & Data Structures
Author's notes

The monotonic stack part clicked pretty fast for me, standard next-smaller-element pattern.

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 price in O(n) time. Iterate through the prices, maintaining a stack of indices with non-decreasing prices; when a smaller or equal price is found, it becomes the discount for the indices on the stack. Track the sum of final prices and collect indices that never receive a discount.

Pro tip: Clarify whether the discount is subtracted from the price or if the final price is the discounted price itself, as ambiguity can lead to incorrect solutions. Also, consider edge cases like empty array or all equal prices to ensure robustness.

1. Understand the problem and clarify requirements

Confirm that the discount for an item is the price of the next cheaper or equal item to its right, and that final price = original price - discount (or 0 if no discount). Ask if the discount can be applied multiple times or if it's a one-time reduction.

2. Choose the right data structure

Recognize that finding the next smaller or equal element for each item is a classic monotonic stack problem. Use a stack to keep track of indices of items that haven't found their discount yet.

3. Iterate and compute discounts

Traverse the prices array. For each price, while the stack is not empty and the current price is less than or equal to the price at the stack's top index, pop the index and apply the discount (subtract current price from that item's price). Push the current index onto the stack.

4. Handle remaining items and compute results

After traversal, any indices left in the stack have no discount, so their final price remains the original. Sum all final prices and collect the indices from the stack as the list of no-discount indices.

5. Analyze complexity and test

Explain that each element is pushed and popped at most once, giving O(n) time and O(n) space. Walk through a small example to verify correctness, including edge cases.

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: next cheaper-or-equal includes equal
  • Edge cases: empty array, single element, all increasing, all decreasing
  • Clarification on discount calculation: final price = original - discount
  • Tracking sum and indices efficiently without extra passes

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