Wrong Answer (Problem Code: COMPILER)

var input_data = '';
process.stdin.on('data',function(chunk){
   input_data += chunk;
});
process.stdin.on('end',function(){
    input_data = input_data.split('\n');
    let test_case = parseInt(input_data[0]),iteration=1,exprsn,stack,loop,count,char;
    while(iteration<=test_case){
        exprsn = input_data[iteration];
        stack = [];count = 0;
        if(exprsn[0] == '>') console.log(0);
        else{
            for(loop = 0;loop<exprsn.length;loop++){
                if(exprsn[loop] == '<') stack.push('<'); //pushing < to stack
                else{
                    if(stack.length === 0) break;
                
                    char = stack.pop(); //popping from stack if found >
                    if(char == '<') count = count + 2;
                }
            }
            console.log(count);
        }
        iteration++;
    }
});

As I am passing the mentioned sample test cases, can anyone help me out to know for which test cases my program is failing?

Question Link ?

1 Like

@nuttela COMPILER Problem - CodeChef

1 Like

Consider the testcase:

1                                                       
<<<<<<<>
1 Like

Your :

is not correct.

Your code is failing in this testcase

1
<<<>

It gives 2, you are doing count+=2. The answer should be 0.

1 Like

@nuttela but as per the question last two brackets is matching the test cases the question then why the answer should be 0?

Prefixes only. :stuck_out_tongue:

@nuttela so till what index we will consider the prefixes because for this test case “<<>>” the output is 4

If the count of '>' exceeds the count of '<' at any point, store the index,
and stop.
Come out of loop and print ( index ).

Or if the Count of them get equal at any point, store the index at the point (keep replacing till loop ends)
Print that.

1
<<<>

It will be not fit any of the above cases and will be zero.

Consider :

1
<<>>

It falls in second case, you stop and find the count to be 3 (index of the last). Print (3 + 1) = 4

Consider :

1
<>>

Falls under first case, Stops and print ( index = 2 )

:slight_smile:

1 Like

Code : here

1 Like

@nuttela it is working thanks for the help

2 Likes