Codesignal Javascript challenge almostIncreasingSequence

Codesignal Javascript challenge almostIncreasingSequence
0

#1

So this is javascript challenge I attempted in code signal.

Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array

, i passed all the test but failed 1 of the hidden test and I am unsure why can anyone confirm this?. The hidden Test is an array containing [1-5000]. Hidden test output as “Empty”, expected to be “true”

this is my code and should logically work. Initially, I thought it has something to do with runtime but looking at the youtube breakdown, the runtime is about the same for the last test. Do you guys think its a bug?

function almostIncreasingSequence(sequence) {

    if(isSequence(sequence)){ //if its sequence by default no need for further checks
        return true;
    }
    
    for(let i = 0; i < sequence.length; i++){
        var sliceCopy = sequence.slice(0);
        
        sliceCopy.splice(i,1);
        if(isSequence(sliceCopy)){
            return true;
        }
    }
    return false;

}

function isSequence(sequence){
  for(var j = 0; j < sequence.length -1; j++){
      if(sequence[j] >= sequence[j+1]){
         return false;
       }
   }
  return true;
}

#2

I have solved this algorithm myself, but I’m not sure why you’re getting a syntax error, I was expecting your code to time out instead. For EVERY element, you remove it and then run through the whole array to check whether it’s an increasing sequence, then you restore the whole array. That’s a lot of work.

You can solve this by running through the array once, using splice just once. In fact, you don’t need to use splice at all. Try setting up an ‘error’ counter and if this exceeds 1 then return false. Return true at the end of the loop should the code reach that point. I suspect the syntax error you’re getting is really a timeout error.