← Microsoft Interview Insights

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

IntermediatePrefer not to say
May 2026

Summary

Microsoft SWE coding round, three problems back to back. Nothing too exotic but the third one tripped me up a bit. Felt okay leaving but not great.

Questions Asked (3)

Q1

Given a list of tasks labeled A-Z and a cooldown period n, find the minimum number of time units needed to run all tasks (same task type must be separated by at least n intervals, idle slots allowed).

Algorithms & Data Structures
Author's notes

I knew this one.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints and edge cases, then propose a greedy approach based on the most frequent task. Derive the formula max((maxFreq-1)*(n+1)+numMaxFreq, totalTasks) and explain why it works, using examples to illustrate.

Pro tip: Mention that this is a classic problem (LeetCode 621) and that the greedy formula is optimal; also note that if n=0, the answer is simply the total number of tasks. This shows you recognize patterns and handle edge cases.

1. Clarify the problem

Ask about constraints: task list size, n range, whether tasks are single characters, and if idle slots are allowed. Confirm that the goal is to minimize total time units.

2. Identify the key insight

Recognize that the most frequent task dictates the schedule structure. The minimum time is at least (maxFreq-1)*(n+1) + (number of tasks with maxFreq).

3. Derive the formula

Explain that the formula accounts for idle slots needed between the most frequent tasks. If there are enough other tasks to fill the gaps, no idle slots are needed, so the answer is max(formula, totalTasks).

4. Validate with examples

Walk through a simple example (e.g., tasks = [A,A,A,B,B,B], n=2) to show how the formula gives the correct answer and why it's optimal.

5. Discuss complexity and edge cases

State that the solution runs in O(N) time and O(1) space (since only 26 task types). Mention edge cases like n=0 or all tasks distinct.

Key Points to Mention

  • Greedy approach based on the most frequent task
  • Formula: max((maxFreq-1)*(n+1) + numMaxFreq, totalTasks)
  • Why idle slots are needed and how they are minimized
  • Time and space complexity: O(N) time, O(1) space
  • Edge cases: n=0, all tasks same, all tasks distinct
  • Alternative approaches (e.g., priority queue simulation) and why the formula is more efficient

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

Q2

Remove duplicates from a sorted integer array in-place using O(1) extra space, returning the count of unique elements.

Algorithms & Data Structures
Author's notes

Warmup problem, two pointers, done.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a two-pointer technique: one pointer (write) tracks the position for the next unique element, and the other (read) scans the array. Since the array is sorted, duplicates are adjacent, so compare the current element with the last unique element and overwrite when different. Return the write pointer as the count of unique elements.

Pro tip: Explicitly state the time and space complexity (O(n) time, O(1) space) and mention that the array is modified in-place, which is a key requirement. Also, handle edge cases like empty arrays gracefully.

1. Clarify and Confirm

Restate the problem to ensure understanding: sorted array, in-place removal, return count of unique elements. Ask if the array can be empty or if there are any constraints on the input size.

2. Explain the Two-Pointer Approach

Describe using two pointers: one for writing unique elements (starting at index 1) and one for reading through the array (starting at index 1). Compare each read element with the previous unique element.

3. Walk Through an Example

Choose a small example (e.g., [1,1,2,3,3]) and manually demonstrate how the pointers move and the array is updated. Show the final array and the returned count.

4. Analyze Complexity

State that the algorithm runs in O(n) time because each element is visited once, and uses O(1) extra space since only two pointers are used.

5. Handle Edge Cases

Mention handling of empty array (return 0) and array with all duplicates (return 1). Also note that the elements beyond the unique count do not matter.

Key Points to Mention

  • Two-pointer technique (read and write pointers)
  • In-place modification without using extra space
  • Leveraging sorted property to detect duplicates
  • Time complexity O(n) and space complexity O(1)
  • Return value is the count of unique elements, not the modified array
  • Edge cases: empty array, single element, all duplicates

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

Q3

Rearrange a string so no two adjacent characters are the same. Return an empty string if it's impossible.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I slowed down.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and edge cases, then propose a greedy approach using a max-heap to always place the most frequent remaining character that differs from the last placed one. If at any point the most frequent character exceeds (n+1)/2, return an empty string as it's impossible.

Pro tip: Mention that this is essentially a scheduling problem and that the greedy approach is optimal; also note that using a max-heap gives O(n log k) time where k is the number of distinct characters, which is efficient.

1. Clarify and Validate

Ask about input constraints (e.g., character set, length) and confirm that rearranging means reordering existing characters. Check if the string length is 0 or 1, which are trivial cases.

2. Check Feasibility

Count character frequencies. If any character appears more than (n+1)/2 times, it's impossible to rearrange, so return an empty string.

3. Choose Data Structures

Use a max-heap (priority queue) to efficiently retrieve the character with the highest remaining frequency. Alternatively, use a frequency array and sort, but heap is more efficient for repeated operations.

4. Greedy Construction

Repeatedly pop the most frequent character, append it to the result if it's different from the last appended character. If it matches, pop the next most frequent, append that, and push the first back with decremented count.

5. Analyze Complexity and Trade-offs

Discuss time complexity: O(n log k) where k is distinct characters, and space O(k). Mention alternative approaches like sorting and interleaving, and why greedy is optimal.

Key Points to Mention

  • Feasibility condition: max frequency <= (n+1)/2
  • Greedy algorithm with max-heap to always pick the most frequent valid character
  • Handling the case where the most frequent character equals the last placed character
  • Time and space complexity analysis
  • Edge cases: empty string, single character, all same characters
  • Proof of optimality: greedy choice ensures no adjacent duplicates if feasible

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