Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,437 views
in Online Assessments by Expert (108,110 points) | 1,437 views

1 Answer

0 like 0 dislike
Best answer

This is a similar question to https://leetcode.com/problems/robot-bounded-in-circle/

 

The only difference is our input for the function is a List and we need to return "YES" or "NO" for reach command based on if we go in a circle or not.

 

My current code which has some bugs. The assesment is over but what is the correct solution?

 

public static List<String> doesCircleExist(List<String> commands) {
    // Write your code here
        int x = 0;
        int y = 0;
        
        int[][] coordinates = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        
        int direction = 0;
        
        for(int i = 0; i < commands.size(); ++i) {
            if(commands[i] == 'L') {
                direction = (direction + 3) % 4;
            }
            else if(commands[i] == 'R') {
                direction = (direction + 1) % 4;
            }
            else {
                x += coordinates[direction][0];
                y += coordinates[direction][1];
            }
        }
        
       if((x == 0 & y == 0) || (direction != 0)) {
           return "YES";
       }
       
       return "NO";
by Expert (108,110 points)