Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Generate All Subsets

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Generate All Subsets. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Write a function that generates all possible subsets of a set.

Implement def generate_subsets(items):, where items is a list of distinct integers. Return a list containing every subset, including the empty subset; preserve the input order within each subset. Any consistent ordering of the returned subsets is acceptable. For example, [1, 2] produces [[], [1], [1, 2], [2]], and [] produces [[]].

Constraints

  • 0 <= len(items) <= 15
  • All elements in items are distinct integers
  • The empty subset must be included
  • Preserve the relative order of elements from items within each subset

Function Signature

def generate_subsets(items):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output