← Microsoft Interview Insights

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

IntermediatePrefer not to say
May 2026

Summary

Microsoft SWE interview that leaned heavily on fundamentals. Three questions back to back, all algorithmic, no system design or behavioral. The kind of session where you either know your stuff cold or you're visibly scrambling.

Questions Asked (3)

Q1

Implement a binary tree node class and write preorder, inorder, and postorder traversals both recursively and iteratively using an explicit stack. Return each traversal as a list and walk through the time and space complexity.

Algorithms & Data Structures
Author's notes

The recursive versions came out fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining a simple TreeNode class with value, left, and right attributes. Then implement recursive traversals first, as they are straightforward, and use them as a reference to implement iterative versions using an explicit stack. Finally, analyze time and space complexity for each approach, noting that all traversals visit each node once (O(n) time) and space depends on tree height (O(h) for recursion/stack, O(n) worst-case).

Pro tip: Mention that iterative traversals can be unified using a stack of (node, state) pairs or by using Morris traversal for O(1) space, but clarify that the explicit stack approach is expected here. Also, emphasize that preorder and postorder iterative are easy with a stack, but inorder requires careful pointer manipulation.

1. Define the TreeNode class

Create a class with a constructor that initializes value, left, and right attributes. Keep it simple and generic.

2. Implement recursive traversals

Write preorder, inorder, and postorder functions that recursively visit left and right subtrees. Use a helper function that appends to a result list.

3. Implement iterative traversals with explicit stack

For preorder, push root and process; for inorder, traverse left while pushing; for postorder, use two stacks or reverse preorder. Ensure each returns a list.

4. Analyze time and space complexity

State that all traversals run in O(n) time. Space is O(h) for recursion and explicit stack, where h is tree height; worst-case O(n) for skewed tree, best-case O(log n) for balanced tree.

5. Test with examples and edge cases

Walk through a small tree (e.g., 1-2-3) to verify outputs. Mention edge cases like empty tree, single node, and skewed tree.

Key Points to Mention

  • Time complexity: O(n) for all traversals because each node is visited exactly once.
  • Space complexity: O(h) for recursion call stack and explicit stack, where h is tree height; worst-case O(n) for skewed tree.
  • Recursive vs iterative trade-offs: recursion is simpler but risks stack overflow; iterative uses explicit stack for control.
  • Inorder iterative algorithm: push all left children, pop and process, then move to right child.
  • Postorder iterative can be done with two stacks or by reversing a modified preorder (root-right-left).
  • Edge cases: empty tree returns empty list; single node returns [value]; skewed tree tests worst-case space.

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

Q2

Given an unweighted graph as an adjacency list and a source node, implement BFS to return the visit order, minimum edge distances from the source, and a parent map for path reconstruction. Also explain how you'd handle disconnected graphs.

Algorithms & Data Structures
Author's notes

This was the one I felt best about.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the graph representation and requirements, then outline BFS using a queue and visited set to track order, distances, and parents. Explain how to handle disconnected graphs by iterating over all nodes and running BFS from each unvisited node, resetting or accumulating results as needed.

Pro tip: Mention that BFS naturally finds shortest paths in unweighted graphs, and discuss trade-offs like using a deque for O(1) pops and the memory cost of storing parents for path reconstruction.

1. Clarify requirements and assumptions

Confirm the graph is unweighted, adjacency list format, and whether the source is guaranteed to be in the graph. Ask if the graph is directed or undirected and if multiple components should be handled.

2. Design BFS core

Use a queue to process nodes level by level, a visited set to avoid revisiting, and maintain arrays/maps for visit order, distances, and parents. Initialize distance of source to 0 and parent to null.

3. Implement BFS traversal

While queue is not empty, dequeue a node, record its visit order, and for each neighbor not visited, mark visited, set distance = current distance + 1, set parent, and enqueue.

4. Handle disconnected graphs

After BFS from the source, check for unvisited nodes. For each unvisited node, run BFS from it, treating it as a new component. Decide whether to return separate results per component or a combined structure with component IDs.

5. Discuss complexity and edge cases

State time complexity O(V+E) and space O(V). Mention edge cases: empty graph, source not present, self-loops, and multiple components. Explain how parent map enables path reconstruction via backtracking.

Key Points to Mention

  • BFS guarantees shortest path in unweighted graphs
  • Use of queue (FIFO) and visited set to avoid cycles
  • Maintaining distance array initialized to -1 or infinity
  • Parent map for path reconstruction (backtrack from target to source)
  • Handling disconnected graphs by iterating over all vertices and running BFS on unvisited ones
  • Time and space complexity: O(V+E) time, O(V) space

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

Q3

Given an array of distinct integers, generate the power set. Provide both a backtracking solution and a purely iterative one using only for-loops, explaining how subsets grow layer by layer. Analyze the complexity.

Algorithms & Data Structures
Author's notes

Backtracking was fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and edge cases, then present the backtracking solution with a clear recursion tree, followed by the iterative solution that builds subsets layer by layer. Explain how each new element doubles the number of subsets, and analyze time and space complexity for both approaches.

Pro tip: Emphasize that the iterative approach naturally generates subsets in increasing size order, which can be useful for certain applications, and mention that the total number of subsets is 2^n, so any solution must take at least O(2^n) time.

1. Clarify and Define

Confirm that the array contains distinct integers and that the order of subsets does not matter. Discuss edge cases like empty array.

2. Backtracking Solution

Explain the recursive backtracking approach: at each index, decide to include or exclude the current element, building subsets incrementally. Trace through a small example to illustrate.

3. Iterative Solution

Describe the iterative method: start with a list containing the empty subset. For each element, create new subsets by adding the element to all existing subsets and append them to the list.

4. Complexity Analysis

Analyze time complexity: both methods generate 2^n subsets, each taking O(n) to copy, resulting in O(n * 2^n) time. Space complexity: O(n * 2^n) to store all subsets, plus O(n) recursion stack for backtracking.

5. Compare and Conclude

Summarize the trade-offs: backtracking is more memory-efficient during generation but recursive; iterative is straightforward and avoids recursion overhead. Mention that both are optimal in terms of output size.

Key Points to Mention

  • The number of subsets is 2^n, so any algorithm must take at least O(2^n) time.
  • Backtracking uses recursion and explores a binary decision tree (include/exclude each element).
  • Iterative approach builds subsets layer by layer, doubling the number of subsets with each new element.
  • Time complexity for both is O(n * 2^n) because each subset takes O(n) to copy.
  • Space complexity is O(n * 2^n) to store all subsets, plus O(n) recursion stack for backtracking.
  • The iterative method can be implemented with simple for-loops and avoids recursion, which may be preferred in environments with limited stack space.

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