PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Microsoft

Implement tree traversals, BFS, and subsets generation

Last updated: Mar 29, 2026

Quick Overview

This question evaluates proficiency with fundamental data structures and algorithms—binary tree traversals (preorder, inorder, postorder), breadth-first search on unweighted graphs, and power-set/subset generation—emphasizing implementation choices between recursive and iterative approaches and required time and space complexity reasoning.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Implement tree traversals, BFS, and subsets generation

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

1) Define a TreeNode class for a binary tree with an integer value and left/right pointers. Implement preorder, inorder, and postorder traversals: (a) recursive versions; (b) iterative versions using an explicit stack (you may use two stacks for postorder). Return the traversal orders as lists and analyze time and space complexity. 2) Given an unweighted graph as an adjacency list and a source node s, implement BFS to return the visitation order, a distance map of minimum edge distances from s, and a parent map to reconstruct shortest paths. Handle disconnected graphs by describing how to traverse all components and produce results for each. 3) Given an array of distinct integers nums, generate the power set (all subsets). Provide (a) a backtracking solution; and (b) an iterative solution using for-loops only (no recursion), explaining how subsets are built layer by layer. Return subsets in any order and analyze complexity.

Quick Answer: This question evaluates proficiency with fundamental data structures and algorithms—binary tree traversals (preorder, inorder, postorder), breadth-first search on unweighted graphs, and power-set/subset generation—emphasizing implementation choices between recursive and iterative approaches and required time and space complexity reasoning.

Related Interview Questions

  • In-Memory URL Shortener: Encode and Decode - Microsoft (medium)
  • Minimum Moves on a Grid with k-Cell Jumps - Microsoft (medium)
  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Compute the Product of an Array Except Self - Microsoft (medium)
|Home/Coding & Algorithms/Microsoft

Implement tree traversals, BFS, and subsets generation

Microsoft logo
Microsoft
Sep 6, 2025, 12:00 AM
mediumSoftware EngineerOnsiteCoding & Algorithms
4
0
  1. Define a TreeNode class for a binary tree with an integer value and left/right pointers. Implement preorder, inorder, and postorder traversals: (a) recursive versions; (b) iterative versions using an explicit stack (you may use two stacks for postorder). Return the traversal orders as lists and analyze time and space complexity.
  2. Given an unweighted graph as an adjacency list and a source node s, implement BFS to return the visitation order, a distance map of minimum edge distances from s, and a parent map to reconstruct shortest paths. Handle disconnected graphs by describing how to traverse all components and produce results for each.
  3. Given an array of distinct integers nums, generate the power set (all subsets). Provide (a) a backtracking solution; and (b) an iterative solution using for-loops only (no recursion), explaining how subsets are built layer by layer. Return subsets in any order and analyze complexity.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Microsoft•More Software Engineer•Microsoft Software Engineer•Microsoft Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.