Ad

Your goal is to write a function that takes in 2 strings and return the winner in rock paper scissors. For example:

dumbRockPaperScissors("Rock", "Paper") // "Player 2 wins"
dumbRockPaperScissors("Paper", "Rock") // "Player 1 wins"
dumbRockPaperScissors("Scissors", "Scissors") // "Draw"

The twist is that your function needs to be as SLOW as you can make it. The 2 rules:

  • No setTimeouts or anyway to artificially inflate times
  • All code must provide some sort of value toward solving the problem
function dumbRockPaperScissors(player1, player2) {

}

This function has already been writte, but is very slow, operating at O(n^2) complexity. Your goal is to optimize this function to be as fast as possible.

const firstNonRepeatingCharacter = (str) => {
    for (let i = 0; i < str.length; i++) {
        let seenDuplicate = false;
        for (let j = 0; j < str.length; j++) {
            if (str[i] === str[j] && i !== j) {
                seenDuplicate = true;
                break;
            }
        }
        if (!seenDuplicate) {
            return str[i];
        }
    }
    return null; // return null if no unique character is found
};