DEV Community

Cover image for Road to Genius: superior #51
Ilya Nevolin
Ilya Nevolin

Posted on

2 2

Road to Genius: superior #51

Each day I solve several coding challenges and puzzles from Codr's ranked mode. The goal is to reach genius rank, along the way I explain how I solve them. You do not need any programming background to get started, and you will learn a ton of new and interesting things as you go.

function backtrack(list, tempList, nums, start) {
    list.push([...tempList]);
    for(let i = start; i < nums.length; i++) {
        tempList.push(nums[i]);
        backtrack(list, tempList, nums, i + 1);
        tempList.pop();
    }
}

function subsets(nums) {
    const list = [];
    backtrack(list, [], nums, 0);
    return list;
}

let A = subsets([1, 2, 1]);
A = A.length

// A = ? (number)
Enter fullscreen mode Exit fullscreen mode

In today's challenge we have to deal with recursive backtracking. The caller function subsets reveals the nature of the code, it's going to create a list of different subsets from a given input. In this case subsets are similar to unique combinations, except they are not required to be unique.

coding challenge extra

For input: [1, 2, 1]
We expect the following subsets:
1
1 2
1 2 1
1 1
2
2 1
1
Enter fullscreen mode Exit fullscreen mode

There are 8 subsets possible so the answer should be as such:
coding challenge answer

If you work out this problem on paper you will find these exact subsets.

By solving these challenges you train yourself to be a better programmer. You'll learn newer and better ways of analyzing, debugging and improving code. As a result you'll be more productive and valuable in business. Get started and become a certified Codr today at https://nevolin.be/codr/

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay