Microsoft JavaScript Tasks



Good day, friends!

I present to you three JS assignments offered to participants in Microsoft's Online Assessment 2019.

Microsoft's Online Assessment is a preliminary selection of candidates for developers. Those who have passed the preliminary selection are invited to an online interview. I found information on the network that exactly one hour was allotted to the solution of problems, but this is not accurate.

I suggest you familiarize yourself with the questions and try to solve them yourself. Detailed comments are welcome.

Thank you so much for solving Matt Clark .

So let's go.

Task number 1


Write a solution function that satisfies the following conditions:

  • the function takes a string S consisting of N letters of the English alphabet
  • the function returns a string that does not contain three identical letters in a row
  • function removes the minimum number of letters from string S

Examples:

  1. S = "eedaaad", the function returns "eedaad". One letter “a” is deleted.
  2. S = "xxxtxxx", the function returns "xxtxx". The same letters can occur in a line more than three times, but not in a row.
  3. S = "uuuuxaaaaxuuu", the function returns "uuxaaxuu".

Assumptions:

  • N is an integer in the range [1..200,000]
  • S consists only of lowercase letters [az]

Decision:
function solution(input) {
    let result = input.slice(0, 2);
    for (let i = 2; i < input.length; i++) {
        if (input[i] !== input[i - 1] || input[i] !== input[i - 2]) {
            result += input[i];
        }
    }
    console.log(result);
    return result;
}
solution('eedaaad'); // eedaad
solution('xxxtxxx'); // xxtxx
solution('uuuuxaaaaxuuu'); // uuxaaxuu

/*
:
-   result,       
-     :
    -           ,    result
    -      ,   ,   
-  result
*/


Task number 2


Write a solution function that satisfies the following conditions:

  • the function takes an array A consisting of N integers
  • the function returns the maximum sum of two numbers whose digits when added add the same amount
  • if there are no such numbers in the array, the function returns -1

Examples:

  1. A = [51, 71, 17, 42], the function returns 93. Here are two pairs of numbers, the digits of which add the same sums: (51, 42) and (17, 71). The sum of the first (largest) pair is 93.
  2. A = [42, 33, 60], the function returns 102. Adding the digits of all numbers in the array gives the same amount. 42 + 60 = 102.
  3. A = [51, 32, 43], the function returns -1, since adding the digits of all numbers gives different amounts.

Assumptions:

  • N is an integer in the range [1..200,000]
  • Each element of array A is an integer in the range [1..1,000,000,000]

Decision:
function sumDigits(num) {
    return String(num).split('').reduce(add);
}

function add(a, b) {
    return Number(a) + Number(b);
}

function descending(a, b) {
    if (a > b) {
        return -1;
    } else if (a < b) {
        return 1;
    } else {
        return 0;
    }
}

function extractHighestTwo(arr) {
    return arr.sort(descending).slice(0, 2);
}

function solution(arr) {
    let sums = {};

    arr.forEach(function(num) {
        sum = sumDigits(num);
        if (sums[sum]) {
            sums[sum].push(num);
        } else {
            sums[sum] = [num];
        }
    });

    result = Object.values(sums)
        .filter(nums => nums.length >= 2)
        .map(extractHighestTwo)
        .map(nums => nums.reduce(add))
        .sort(descending)[0];

    console.log(result || -1);
    return result || -1;
}
solution([51, 71, 17, 42]); // 93
solution([42, 33, 60]); // 102
solution([51, 32, 43]); // -1

/*
:
-   
-    
    -     
    -        :
        -         ,  
    -        :
        -    ,  ,  ,    
-      (   "",      )
-  ,    
-   ,    ,   ,  
-       
-           -1,     
*/

/*
   :
function solution(arr) {
    let sums = {};

    arr.forEach(num => {
        sum = String(num).split('').reduce((a, b) => Number(a) + Number(b));
        
        sums[sum] ? sums[sum].push(num) : sums[sum] = [num];
    });

    result = Object.values(sums)
        .filter(nums => nums.length >= 2)
        .map(nums => nums.sort((a, b) => b - a).slice(0, 2))
        .map(nums => nums.reduce((a, b) => a + b))
        [0]

    console.log(result || -1);
    return result || -1;
}
solution([51, 71, 17, 42]); // 93
solution([42, 33, 60]); // 102
solution([51, 32, 43]); // -1
*/


Task number 3


Given the string S, consisting of N letters "a" and / or "b". In one action, you can change any letter to the opposite (“a” to “b” or “b” to “a”).

Write a solution function that satisfies the following conditions:

  • the function takes the specified string S
  • the function returns the number of actions required to get a string that does not contain three identical letters in a row

Examples:

  1. S = “baaaaa”, the function returns 1. A string that does not contain three identical letters in a row (“baabaa”) can be obtained in one action.
  2. S = "baaabbaabbba", the function returns 2. In two actions, you can get four "valid" lines, for example, "bbaabbaabbaa".
  3. S = "baabab", the function returns 0.

Assumptions:

  • N is an integer in the range [0..200,000]
  • string S contains only the letters “a” and / or “b”

Decision:
function numberOfMovesForStreak(streak) {
    let length = streak.length - 2;

    while (length % 3 !== 0) {
        length++;
    }

    return length / 3;
}

function solution(input) {
    const streaks = [];
    let temp = '';

    for (let i = 0; i < input.length; i++) {
        if (input[i] === input[i - 1]) {
            temp += input[i];
        } else {
            if (temp.length > 2 && temp.length !== 0) {
                streaks.push(temp);
            }
            temp = input[i];
        }
    }
    if (temp.length > 2) {
        streaks.push(temp);
    }

    console.log(streaks.map(numberOfMovesForStreak).reduce(((a, b) => a + b), 0));
    return streaks.map(numberOfMovesForStreak).reduce(((a, b) => a + b), 0);
}
solution('baaaaa'); // 1
solution('baaabbaabbba'); // 2
solution('baabab'); // 0

/*
 ,      :
   3-5   ,   1 
 6-8   - 2 
 9-11  - 3 
:
-     ,   3     
-  ,   3     ,  
    - ""   :
        -   
        -  2
        -     ,  3
        -   3   
    -   
*/


Thank you for attention.

Happy coding!

All Articles