← Microsoft Interview Insights

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

Intermediate
May 2026

Summary

Microsoft SWE coding round, one question the whole time. The problem looked like a simple array scan but the full-price tracking part tripped me up more than I expected.

Questions Asked (1)

Q1

Given a prices array, compute each item's final selling price by finding the first later item with a price less than or equal to it and subtracting that value as a discount. Output the total of all final prices and the indices of items sold at full price.

Algorithms & Data Structures
Author's notes

The core scan isn't bad once you think monotonic stack, but I got snagged on the output format.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem and edge cases, then propose an efficient monotonic stack solution that finds the next smaller or equal element for each price in O(n) time. Explain how to compute the final prices and identify full-price items, and discuss the total sum and indices.

Pro tip: Mention that a monotonic stack is ideal for 'next smaller or equal element' problems, and that using a stack of indices allows you to update the result array in place while preserving original order.

1. Clarify the problem

Restate the problem in your own words and confirm details: discount is the first later item with price <= current, final price = current - discount (or current if none), output total sum and indices of items sold at full price.

2. Discuss brute force and optimize

Acknowledge the O(n^2) brute force approach, then propose an O(n) solution using a monotonic stack to efficiently find the next smaller or equal element for each price.

3. Explain the monotonic stack algorithm

Iterate through prices, maintaining a stack of indices with non-decreasing prices. For each price, pop indices where the current price is <= the price at that index, and set the discount for those indices to the current price.

4. Compute final prices and full-price indices

After processing, any indices left in the stack have no discount (full price). Compute the total sum of final prices and collect the indices of items sold at full price.

5. Analyze complexity and edge cases

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

Key Points to Mention

  • Monotonic stack technique for next smaller or equal element
  • Time complexity O(n) and space complexity O(n)
  • Handling duplicates correctly (using <= condition)
  • Computing total sum and identifying full-price items
  • Edge cases: empty array, single element, strictly increasing/decreasing prices
  • In-place modification of a result array to store final prices

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