Interview Questions

  • View all interview questions
  • blank
  • Find all string combinations consisting only of 0, 1 and ?
    For this popular algorithm interview question, the input will be a string consisting only of the characters 0, 1 and ?, where the ? acts as a wildcard that can be either a 0 or 1, and your goal is to print all possible combinations of the string. For example, if the string is "011?0" then your program should output a set of all strings, which are: ["01100", "01110"].

    Algorithm




    You need to be a premium member to see the rest of this question and code.

    Become a Premium Member

    With our large collection of challengs, tutorials, and solutions, we make it easy for you to become a better coder, prepare for interviews, and learn new skills from more experienced coders.

    “I have my final coding interview with Fullstack Academy tomorrow, and Coderbyte has been an invaluable tool to help me progress as a
    developer.” ― Josh Aharonoff
    • 200+ Coding Challenges
    • Mock Interview Questions
    • 500,000+ Code Solutions
    • Algorithm Tutorials
    • Interview Prep Courses
    mrdaniel published this on 11/24/15 | array, math, combination, Google
    Comments
  • +
  • 1
  • -
  • What's wrong with this way?
    /*
        All string combos of 1, 0 and ?
        11?1 => [1101, 1111]
        10?? => [1000, 1010, 1011, 1001]
    */
    
    
    var all = [];
    
    function allCombos(str) {
        let idx = str.indexOf('?');
        
        if (idx < 0) {
            all.push(str);
            return;
        }
    
        var splitStr = str.split('');
        var addZero = splitStr.slice();
        var addOne = splitStr.slice();
    
        addZero.splice(idx, 1, '0');
        addOne.splice(idx, 1, '1');
    
        allCombos(addZero.join(''));
        allCombos(addOne.join(''));
    }
    
    allCombos('10??');
    
    all;
    
  • +
  • 0
  • -
  • Mine was this
    function combination(arr) {
      let elems = [];
      var fn = function(newElem, index) {
        if (newElem.length == arr.length && newElem.indexOf('?') == -1) {
          elems.push(newElem);
        }
        let position = newElem.indexOf('?');
        if (position > 0) {
          let stringElement = newElem.substring(0, position);
          fn(stringElement + '0' + newElem.substring(position + 1, newElem.length), position + 1);
          fn(stringElement + '1' + newElem.substring(position + 1, newElem.length), position + 1);
        }
      };
      fn(arr, -1);
      return elems;
    }
  • +
  • 0
  • -
  • I tried this way..
    function power(arrayStart,iStart,strEnd,arrayRes){
    	for(var i=iStart;i<arrayStart.length;i++){
      	if(arrayStart[i]==='?'){
        	power(arrayStart,i+1,strEnd+'0',arrayRes);
          power(arrayStart,i+1,strEnd+'1',arrayRes);
          return;
        }else{
        	strEnd+= arrayStart[i];
        }
      }
      arrayRes.push(strEnd);
    }
    
    function wild(str){
    	var arrayRes= [];
    	power(str.split(''),0,'',arrayRes);
      
            return arrayRes;
    }
    
    wild('10?1?');
    
  • +
  • 0
  • -
  • Without recursion...
    function wildCard(str) {
      var list = [];
      var wild = [];
    
      if (str.match(/\?/)) wild.push(str);
    
      while (wild.length) {
        var wildStr = wild.pop();
    
        for (var i = 0; i < 2; i++) {
          var aux = wildStr.replace(/\?/, i);
          if (aux.match(/\?/)) wild.push(aux);
          else list.push(aux);
        }
      }
    
      return list;
    }
    
    wildCard('10?1?');
    
    Login to submit a comment