Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Implement a function that returns all subsets of a given array.

Hard

Problem Statement

Implement a function that returns all subsets of a given array.

Sample Input

[ 1, 2, 3 ]

Sample Output

[ [], [ 1 ], [ 2 ], [ 3 ], [ 1, 2 ], [ 1, 3 ], [ 2, 3 ], [ 1, 2, 3 ] ]

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
// Generate all subsets of an array function getSubsets(arr) { // Initialize an array with an empty subset // Loop through elements, generating new subsets by adding each element to existing subsets } console.log(getSubsets([ 1, 2, 3 ]));
Log in or sign up to run or submit your code.Log inSign up
Input
[ 1, 2, 3 ]
Expected Output
[ [], [ 1 ], [ 2 ], [ 3 ], [ 1, 2 ], [ 1, 3 ], [ 2, 3 ], [ 1, 2, 3 ] ]