Ads Top

Interview Practice: Validating A Subsequence Solution

Is the given sequence a subsequence explanation

Given an array [5, 1, 22, 25, 6, -1, 8, 10]
And a sequence [1, 6, -1, 10]

Is the sequence a subsequence of the array. Meaning, if you take the order of the subsequence whic is "1, 6, -1, 10", can you find that in the initial array. If you look at the given array, you can find the subsequence in order if you drop the other values.

If you try another subsequence such as

[1, 6, -1, 1]

Notice that the last digit in the array is 1 instead of 10, that would not be a subsequence of the initial array.

If the result is a subsequence, return true. If it is not a subsequence, return false.

Solution Pseudo Process

Initialize the sequence variable and set to 0
Look over the given array. for .... of
Break out if sequenceIndex == sequence.length
If sequence[sequenceIndex] == value
    increase sequenceIndex and continue looping through array
When done with the loop (or breaking out of the loop early)
    return sequenceIndex === sequence.length

It will return true if it is a subsequence or false if it is not

No comments:

Powered by Blogger.