Compiler and Parsers

Why my code says WA i cannot get.
Please Help

import java.io.*;



class Compiler

{

    static String a[];
    static int pos;
    
    public static void main(String args[])throws Exception
    {
        
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int t=Integer.parseInt(br.readLine());
        
        for(int z=0;z<t;z++) {
            a=new String[1000000];
            long l=0;
            pos=0;
            String s=br.readLine();
            for(int i=0;i<s.length();i++) {
                if(s.charAt(i)=='<')push(""+s.charAt(i));
                else {
                    if(pos>0) {
                        String k=pop()+">";
                        l=Math.max(l,k.length());
                        if(pos>0)a[pos-1]=a[pos-1]+k;
                    }
                }
            }
            System.out.println(l);
            
            
        }
        
    }
    static void push(String s) {
        a[pos++]=s;
    }
    static String pop() {
        String k=a[pos-1];
        a[pos-1]=null;
        pos--;
        return k;
    }
    
}

When ever you get

pos<0 

shouldn’t you end the loop? so i think you have to keep

(i<s.length())&&(pos>=0)

that might help… i think so. (i don’t know java, so i am not sure of the boolean condition)

bro there is nothing called “pos<0” and this is working in all the test cases i have made and of the question.

then try these cases::

<><>><

<<<><<>>>

i checked your code in ideone.com, which gave wrong responses for these cases

@eightnoteight for your first testcase the ans should be 2 or anything else??
and for the second it must be 8?
what is the problem?

the first case should give 4, but your code gives 2

the second case should give 0, but your code gives 8

Please help. I have confusion, I thought that we need to find the longest valid sequence in the expression as the test case:
<>>> gives 2 even though full expr is invalid.
So,

<><> should give 4
<>< should give 2
<<<><>> should give 6

my submission: https://www.codechef.com/viewsolution/36060462

Is this right?