genomic range query codility 100% correct javascript solution

Genomic Range Query – Codility 100% Correct Javascript Solution

The genomic range query problem can be solved efficiently if we treat the sequances as a string instead of an array.

The simplest solution I found is to exatract the substring between P[k] and Q[k] and analyse try to find the lowest impact element on each substring. Start from A, continue with C and G. If any of these are found, skip to the next K and save the impact. If none of them are found, then the impact is maximum (4) so add that to the results array.

Here is my Javascript solution with complexity O(N + M):

function solution(S, P, Q) {
    results = []
    impact = {'A':1, 'C':2, 'G':3, 'T':4}
    for (let i=0; i<P.length;i++){
        let analyze = S.substring(P[i],Q[i]+1)
        if (analyze.indexOf('A')!== -1) {
            results[i] = 1
        if (analyze.indexOf('C')!== -1) {
            results[i] = 2
        if (analyze.indexOf('G')!== -1) {
            results[i] = 3
        results[i] = 4

    return results

You can find the report here.

Leave a Reply