## Interview Question for Front-end Software Engineers

Country: United States

Comment hidden because of low score. Click to expand.
0
of 0 vote

I am not sure about the exact problem.

But from example it seems that you have to write an algorithm, with all permutation and combinations whose sum of digits is given number.

Comment hidden because of low score. Click to expand.
0
of 0 vote

This type of problem is typically known as Partition problem. The number of partitions of a number grows exponentially. I read somewhere that Its an NP-Complete problem. Here are few references that you can check to know more about it.

math.stackexchange.com/questions/217597/number-of-ways-to-write-n-as-a-sum-of-k-nonnegative-integers

en.wikipedia.org/wiki/Partition_%28number_theory%29#Intermediate_function

stackoverflow.com/questions/400794/generating-the-partitions-of-a-number#400810

Some of these links will provide you with the implementation in some lang. You can go through the math for wikipedia link.

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````function countSteps(n){
var counts = 0;
function partition(arr, last ,m){
var tempArr;
if(m > 1){
for(var i = last || 1; i <= m/2; i++){
tempArr = arr.slice(0);
if(m-i > i){
tempArr.push(i);

if(m-i > 1){
partition(tempArr.slice(0), i, m-i);
}

tempArr.push(m-i);
}else if(m-i == i){
tempArr.push(i);
tempArr.push(m-i);
}else{
return
}
}
}else{
tempArr = arr.slice(0);
tempArr.push(m);
}

if(tempArr){
counts += 1;
console.log(tempArr);
}
}
partition([], null, n);
return counts;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

var all = [];

function findStep(n) {
if (n===1){
console.log(1);
} else {
_findIt(n, []);
}

all.sort();
var prev = [];
for(var i=0; i<all.length; i++){
var curr = all[i];
if (curr.join() == prev.join()){
all.splice(i,1);
i--;
} else {
prev = curr;
}
}

console.log(all);
console.log(all.length);
}

function getSum (arr){
return arr.reduce(function(a,b){
return a+b;
},0);
}

function _findIt(n, result){
var currentSum = getSum(result);
for(var i = 1; i <= n; i++){
var nextSum = currentSum + i;
if (nextSum === n){
result.push(i);

result.sort();
//console.log(result.join());
all.push(result);
return;
} else {
var newArray = result.slice(0);
newArray.push(i);
_findIt(n, newArray);
}
}
}

findStep(4)

Comment hidden because of low score. Click to expand.
0
of 0 vote

48 ways

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````function findSteps(n){
var counter = 1 ;
for(var i = 1 ; i < n ; i++){
for(var j = i; j < n ; j++){
if(i+j <= n){
counter++;
}
}
}
console.log(counter);
}

findSteps(2);``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````function findSteps(n) {
let count = 0
// think of 'd' as number of digits
for (let d = n; d > 0; d--) {
count += (n - d - Math.ceil(n/d) + 2)
}
return count
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````function findSteps(n) {
let count = 0
// think of 'd' as number of digits
for (let d = n; d > 0; d--) {
count += (n - d - Math.ceil(n/d) + 2)
}
return count
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````function findSteps(n) {
let count = 0
// think of 'd' as number of digits
for (let d = n; d > 0; d--) {
count += (n - d - Math.ceil(n/d) + 2)
}
return count
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.