Shopflo Frontend Interview - R1

Palak BansalPalak Bansal
2 min read

I recently interviewed for Shopflo, and here is all the information that I can provide.

  1. Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.

    Each number in candidates may only be used once in the combination.

    Note: The solution set must not contain duplicate combinations.

    Example 1:

    Input: candidates = [10,1,2,7,6,1,5], target = 8 Output: [ [1,1,6], [1,2,5], [1,7], [2,6] ] Example 2:

    Input: candidates = [2,5,2,1,2], target = 5 Output: [ [1,2,2], [5] ]

    Constraints:

    1 <= candidates.length <= 100 1 <= candidates[i] <= 50 1 <= target <= 30

var combinationSum2 = function(candidates, target) {
    candidates.sort((a, b)=> a-b);
    let ans = [];
    function helper(sum, idx, arr){
        if(sum===0){
            ans.push([...arr]);
            return;
        }
        if(sum<0) return;

        for(let i = idx;i<candidates.length;i++){
            if(i!==idx && candidates[i]===candidates[i-1]) continue;
            helper(sum-candidates[i], i+1, [...arr, candidates[i]])
        }
    }
    helper(target, 0, []);
    return ans;
};

const candidates = [10,1,2,7,6,1,5]; 
const target = 8;

console.log(combinationSum2(candidates, target))
  1. const join = (a,b,c) => {

    return ${a}_${b}_${c} }

    const curriedJoin = curry(join)

    console.log(curriedJoin(1)(2,3)); //1_2_3

    console.log(curriedJoin(1,2)(3)); //1_2_3

    console.log(curriedJoin(1)(2)()()(3)); //1_2_3 console.log(curriedJoin(1,2,3)); //1_2_3

// Define the curry function
function curry(fn) {
  // The curried function will return a new function to collect the arguments
  return function curried(...args) {
    // If enough arguments are collected, call the original function
    if (args.length >= fn.length) {
      return fn.apply(this, args);
    } else {
      // Otherwise, return a new function to collect more arguments
      return function(...nextArgs) {
        return curried.apply(this, args.concat(nextArgs));
      };
    }
  };
}

// Define the join function
const join = (a, b, c) => {
  return `${a}_${b}_${c}`;
}

// Curry the join function
const curriedJoin = curry(join);

// Test cases
console.log(curriedJoin(1)(2, 3)); // 1_2_3
console.log(curriedJoin(1, 2)(3)); // 1_2_3
console.log(curriedJoin(1)(2)()()(3)); // 1_2_3
console.log(curriedJoin(1, 2, 3)); // 1_2_3

Thank you for Reading!

0
Subscribe to my newsletter

Read articles from Palak Bansal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Palak Bansal
Palak Bansal

Experienced Frontend Developer with a demonstrated history of working in the financial services industry along with having 5+ years of hands on professional experience efficiently coding websites and applications using modern HTML, CSS and Javascript along with their respective frameworks viz.Vue.JS, React.JS. Instituting new technologies and building easy to use and user-friendly websites is truly a passion of mine. I actively seek out new libraries and stay up-to-date on industry trends and advancements. Translated designs to frontend code along with streamlining existing code to improve site performance. Collaborated with UX designers and Back End Developers to ensure coherence between all parties. Also tested some feature prototypes for bugs and user experience.