Tiktok Generic OA ( Nov 20 to 24,2023)
Anonymous User
436

I am trying to work on this OA problem which I got. I was able to pass only 5/15 test case with the code below. I think, there might be an issue with overflow. Looking forward to get some help related to this question. Please let me know on how to proceed on this or should i use BigInteger to solve these kinds of problems ?

image

This was the code that i used.

public class _03AuthorizeC {

    public static void main(String[] args) {
        List<List<String>> res = new ArrayList<>();
		// expected answer for this test case should be 1 , but it is coming as 0 
        List<String> event1 = new ArrayList<>(Arrays.asList("setPassword","QtTU703bL"));

        List<String> event2 = new ArrayList<>(Arrays.asList("authorize","134633168"));


        res.add(event1);
        res.add(event2);

        List<Integer> ans = authEvents(res);
        System.out.println(ans);
    }
    public static List<Integer> authEvents(List<List<String>> events)
    {
        List<Integer> res = new ArrayList<>();
        String currentPassword = "";
        List<Double> ansRange = new ArrayList<>();
        // for ( int i = 0;i < events.l
        for ( int i = 0; i< events.size(); i++)
        {
            List<String> event = events.get(i);
            if ( event.get(0).equals("setPassword"))
            {
                ansRange.clear();
                ansRange = hash(event.get(1));  // which correspondings to password
            }
            else if ( event.get(0).equals("authorize")){
                System.out.println(ansRange);
                System.out.println(event.get(1));
               Double guessedPassword = Double.parseDouble(event.get(1));

               if (checkPasswordMatch(ansRange, guessedPassword))
               {
                   res.add(1);
               }
               else{
                   res.add(0);
               }
            }
        }

        return res;
    }

    private static boolean checkPasswordMatch(List<Double> ansRange, Double guessedPassword) {

        if ( Double.compare(guessedPassword,ansRange.get(0))== 0)
        {
            return true;
        }
        if ( Double.compare(guessedPassword,ansRange.get(1)) >0 && Double.compare(guessedPassword,ansRange.get(1))  <0 )
        {
            return true;
        }
        return false;
    }

    private static List<Double> hash(String s) {

        Double exactAns =0D;
        Double lowRange = 0D;
        Double highRange = 0D;
//        bigInteger  =
            for ( int i = 0; i< s.length(); i++)
            {
                exactAns += ((int)(s.charAt(i))%1_000_000_007)*((Math.pow(131,s.length()-i))%1_000_000_007);
                lowRange += ((int)(s.charAt(i))%1_000_000_007)*((Math.pow(131,s.length()-i))%1_000_000_007);

                highRange = highRange%1_000_000_007 + (((int)(s.charAt(i))%1_000_000_007)*((Math.pow(131,s.length()-i))%1_000_000_007))%1_000_000_007;

//                System.out.println(bigInteger);
                lowRange = lowRange%1_000_000_007;
                highRange = highRange%1_000_000_007;
                exactAns = exactAns%1_000_000_007;
            }
            lowRange += 48;
            highRange += 127;
            List<Double> ans = new ArrayList<>();
            ans.add(exactAns);
            ans.add(lowRange);
            ans.add(highRange);
            return ans;

    }

}
Comments (4)