← Questions

Subsets, with and without backtracking

0
MicrosoftCodingLast asked Sep 2025

A coding interview question asked in Microsoft's coding round, last asked Sep 2025. It tests backtracking, bit manipulation and trees.

Given an array of distinct integers, return all its subsets (the power set), in any order. Write it twice: once with backtracking, and once iteratively with loops only and no recursion.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • subsets_backtracking(nums: list[int]) -> list[list[int]]
  • subsets_iterative(nums: list[int]) -> list[list[int]]

Example

[1, 2, 3]  ->  [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]   # any order
[]         ->  [[]]

Constraints

  • 0 <= len(nums) <= 16; values are distinct.

Clarifying questions

  • Does the order of subsets, or of elements inside a subset, matter?
  • Is the empty subset included?
  • Can values repeat? (Here they cannot.)

What a strong answer covers

  • Backtracking: at each index choose to take the element or skip it, or loop over the next element to add; record the current subset at each node.
  • Iterative layering: start with [[]]; for each element, add a copy of every existing subset with that element appended.
  • Bit masks: each number from 0 to 2^n - 1 selects one subset.
  • O(n · 2^n) time and output size, and why no algorithm can do better.
  • Tests: empty input, one element, and a count check of 2^n.

Follow-ups

  • The input may contain duplicates; return each distinct subset once.
  • Return only the subsets of size k.
  • In the same round, expect basic tree and graph warm-ups: pre-order, in-order and post-order traversal of a tree class you define, recursively and with an explicit stack, and a BFS that returns visiting order, distances and parents.

Where it's asked

  • Microsoft · Coding · Last asked Sep 202560 min · Ten to fifteen minutes of behavioral questions, then coding in HackerRank where you write a main and make the tests pass; usually three rounds · No AI unless the interviewer invites it; some 2026 loops add a round where you bring your own AI agent

What it tests

backtracking · bit manipulation · trees

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding