← Microsoft Interview Insights

Microsoft·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
May 2026

Summary

Microsoft SWE coding round, one problem the whole session. The question was a discount/revenue calculation thing built on a monotonic stack, which I knew going in was a classic pattern but still had to think through the two-part output carefully.

Questions Asked (1)

Q1

You're given an array of item prices sold left to right. For each item, find the first item to its right with a price less than or equal to it; that difference is the discounted selling price. If no such item exists, it sells at full price. Return both the total revenue and the list of indices that sold at full price.

Algorithms & Data Structures
Author's notes

Two-part return value tripped me up more than the algorithm itself.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem requirements and edge cases, then propose an efficient solution using a monotonic stack to find the next smaller or equal element for each price. Explain how to compute the discounted prices and total revenue, and identify indices that sell at full price.

Pro tip: Emphasize the O(n) time complexity of the monotonic stack approach and discuss potential pitfalls like handling equal prices correctly (using <=) and edge cases such as empty arrays or single elements.

1. Understand the problem

Restate the problem in your own words and confirm details: for each item, find the first item to its right with price <= current price; discount is the difference; if none, sell at full price. Return total revenue and list of indices with full price.

2. Discuss brute force and optimal approach

Mention that a brute force O(n^2) solution checks each element's right side, but an O(n) solution using a monotonic stack is optimal. Explain that the stack maintains indices of items waiting for a smaller or equal price to their right.

3. Detail the monotonic stack algorithm

Iterate through prices; while stack is not empty and current price <= price at stack top, pop and record the discount for that index. Push current index. After iteration, remaining indices in stack have no discount and sell at full price.

4. Compute total revenue and full-price indices

For each item, if a discount is found, add (price - discount) to total revenue; otherwise, add full price and record the index. Return total revenue and the list of indices.

5. Analyze complexity and edge cases

State time complexity O(n) and space O(n). Discuss edge cases: empty array, single element, all increasing prices, all decreasing prices, and duplicate prices.

Key Points to Mention

  • Monotonic stack technique for next smaller or equal element
  • Time and space complexity analysis (O(n) time, O(n) space)
  • Handling of equal prices (using <= condition)
  • Edge cases: empty array, single element, strictly increasing/decreasing prices
  • Correct computation of discounted price as difference between current and next smaller/equal price
  • Returning both total revenue and list of indices that sold at full price

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