Categories

# Javascript Interview Questions(Part-4)

JavaScript Coding interview questions with solutions:-

Question1:-

`You would like to set a password for a bank account. However, there are three restrictions on the format of the password:it has to contain only alphanumerical characters (a−z, A−Z, 0−9);there should be an even number of letters;there should be an odd number of digits.You are given a string S consisting of N characters. String S can be divided into words by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.For example, given "test 5 a0A pass007 ?xy1", there are five words and three of them are valid passwords: "5", "a0A" and "pass007". Thus the longest password is "pass007" and its length is 7. Note that neither "test" nor "?xy1" is a valid password, because "?" is not an alphanumerical character and "test" contains an even number of digits (zero).Write a function:int solution(char *S);that, given a non-empty string S consisting of N characters, returns the length of the longest word from the string that is a valid password. If there is no such word, your function should return −1.For example, given S = "test 5 a0A pass007 ?xy1", your function should return 7, as explained above.Assume that:N is an integer within the range [1..200];string S consists only of printable ASCII characters and spaces.In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.`

Solution1:-

`function solution(S) {    // write your code in JavaScript (Node.js 6.4.0)    var pwds = S.split(" ");    var pwdLen = -1;    pwds.forEach(function(pwd){        if(pwd.match(/^[a-zA-Z0-9]*\$/)){            if(pwd.length%2!=1)              return;            var chrCnt = pwd.match(/[a-zA-Z]/g || []).length;            if(chrCnt%2!=0)              return;            if (pwd.length > pwdLen)               pwdLen = pwd.length;          }        });   return pwdLen;}`

Question2:-
Given four integers, display the maximum time possible in 24 hour format HH:MM. For example, if you are give A = 1, B = 9, C = 9, D = 2 then output should be 19:29. Max time can be 23:59 and min time can be 00:00.

If it is not possible to construct 24 hour time then return error. For example, given A = 1, B = 9, C = 7, D = 9 an error should be returned since minimum time represented by these integers is 17:99 which is “NOT POSSIBLE”?

Solution2:-

`function solution(A, B, C, D) {   var arr = [];   var digit = [];   var timeString = "";   //create array from given numbers   arr.push(A);   arr.push(B);   arr.push(C);   arr.push(D);   digit = findMax(arr,2);   digit = digit==2?findMax(arr,3):findMax(arr,9);   digit = findMax(arr,5);   digit = findMax(arr, 9);   //final number   if(digit ==-1 ||digit ==-1||digit ==-1||digit ==-1){        return "NOT POSSIBLE";       }    timeString = digit+""+digit+":"+digit+""+digit;      return timeString;}//finding the less than equal number and return itfunction findMax(arr, find){    if(arr.length!=4)    {        return -1;        }    var numToFind = -1;    var indexToRemove = -1;    //iterate arrary    for(var i = 0; i < arr.length;i++){        if(arr[i] <= find)        {            if(arr[i]> numToFind)            {              numToFind = arr[i];              indexToRemove =i;                }            }        }    if (indexToRemove == -1)      return -1;    arr[indexToRemove] = -1;    return numToFind;    }`

Question3:-Given DOM tree I need to find the maximum depth of the nested ul/ol tags.
Example:-

`<ul>  <li>Item:     <ol>       <li>Point:         <div>           <ul>             <li>elem1</li>           </ul>         </div>       </li>     </ol>  </li>  <li>elem2</li></ul><ul>  <li>simple list1</li></ul><ul></ul>The depth would be 3`

Solution3:-

`function solution() {    var len, max_depth=0;    \$('li:not(:has(ol)):not(:has(ul))').each(function(){            len = \$(this).parents('ul,ol').length;            if(len > max_depth)              max_depth =len;        });   //console.log(max_depth);    return max_depth;}`

More Interview Questions