Interview Question






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

read about std::next_permutation

- Alok September 22, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function take_out_char( str, chr ) {
    // TODO: use buffer????
    let result = '';
    for( let n=0; n<from.length; n++ ) {
        if( str[n]!=chr ) {
            result += str[n];
        }
    }
    return result;
}
function get_chars_less_or_equal_to( str, chr ) {
    // TODO: use buffer????
    let result = '';
    for( let n=0; n<str.length; n++ ) {
        if( str[n]<=chr ) {
            result += str[n];
        }
    }
    return result;
}
function factorial( n ) {
    if( n==1 ) return 1;
    return n*factorial(n-1);
}
function count_number_of_strings_between( from, to ) {
    if( from.length!=to.length )
        throw Error("String lengths do not match");
    if( from.length==0 )
        return 0;
    let result = 0;
    // Let's find the number of possible variations with given first character of "to"
    let tmp = get_chars_less_or_equal_to(from,to[0]);
    for( let k=0; k<tmp.length; k++ ) {
       if( tmp[k]==to[0] ) {
           // In this case we need to fix the first char of the "fitting" combination to tmp[k]
           // and count the number of strings between (from without the tmp[k]) and
           // (to without to[0])
           result += count_number_of_strings_between(take_out_char(from,tmp[k]),to.substring(1));
       }
        else {
            // Here tmp[k] is less than to[0].
            // in this case we can make tmp[k] to be the first character and we do not
            // care what the rest of the string is. The number of all possible "rests" is
            // the factorial
            result += factorial(from.length-1);
        }
    }
    return result;
}

let from = 'abc';
let to   = 'ddd';
// Subtract 1 to avoid counting string from itself
console.log(count_number_of_strings_between(from,to)-1);

- fatcat September 23, 2018 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More