• Antriksh Jain

Amazon OA Assessment

Updated: Dec 4, 2021

1. Robot Bounded In Circle

Problem Statement: On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:

  • "G": go straight 1 unit;

  • "L": turn 90 degrees to the left;

  • "R": turn 90 degrees to the right.

The robot performs the instructions given in order, and repeats them forever.

Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.

Example 1:
Input: instructions = "GGLLGG"
Output: true
Explanation: The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0).
When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.

Example 2:
Input: instructions = "GG"
Output: false
Explanation: The robot moves north indefinitely.

Example 3:
Input: instructions = "GL"
Output: true
Explanation: The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ...
Constraints:
1 <= instructions.length <= 100
instructions[i] is 'G', 'L' or, 'R'.

public boolean isRobotBounded(String instructions) {
        if (instructions.length() == 0)
            return false;
        int x = 0;
        int y = 0;  // initial points of the robot
        String directions = "North"; 
                    
         for (char ch: instructions.toCharArray()) {
            if (ch == 'G') {
                if (directions.equals("North"))
                    y += 1;
                else if (directions.equals("South"))
                    y -= 1;
                else if(directions.equals("East"))
                    x += 1;
                else
                    x -= 1;
            }
            else if (ch == 'L') {
                if (directions.equals("North"))
                    directions = "West";
                else if (directions.equals("West"))
                    directions = "South";
                else if (directions.equals("South"))
                    directions = "East";
                else directions = "North";
            }
            else if (ch == 'R') {
                if (directions.equals("North"))
                    directions = "East";
                else if (directions.equals("East"))
                    directions = "South";
                else if (directions.equals("South"))
                    directions = "West";
                else directions = "North";
            }
        }
        if (x == 0 && y == 0)
            return true;
        if (directions.equals("North"))
            return false;
        return true;
    }
        
        
    
2.

Problem statement- Find out the number of competitors Method accepts five arguments -

numCompetitors, an integer representing the number of competitors for the Echo device;

topNCompetitors, is an integer representing the maximum number of competitors that Amazon wants to identify;

competitors, a list of strings representing the competitors;

numReviews, an integer representing the number of reviews from different websites that are identified.

reviews, a list of string where each element is a string that consists of space-separated words representing user reviews.

Expected output: Return a list of strings representing Amazon's top N competitors in order of most frequently mentioned to least frequent.

Additional input:

The comparison of strings is case-insensitive. If the value of topNCompetitors is more than the number of competitors discussed in the reviews then output the names of only the competitors mention.

If competitors have the same count (e.g. newshop=2, shopnow=2, mymarket=4), sort alphabetically. topNCompetitors=2, Output=[mymarket, newshop]



Example
Input:
numCompetitors=6
topNCompetitors = 2
competitors = [newshop, shopnow, afashion, fashionbeats, mymarket, tcellular]
numReviews = 6
reviews = [
"newshop is providing good services in the city; everyone should use newshop",
"best services by newshop",
"fashionbeats has great services in the city",
"I am proud to have fashionbeats",
"mymarket has awesome services",
"Thanks Newshop for the quick delivery"]

Output
["newshop", "fashionbeats"]

Explanation
"newshop" is occurring in 3 different reviews. "fashionbeats" is occuring in 2 different user reviews and "mymarket" is occurring in only 1 review.


public static List<String> topNumCompetitors(int numCompetitors, int topNCompetitors, List<String> competitors, int numReviews, List<String> reviews) {
	List<String> res = new ArrayList<>();
	// Using set just for fast search
	Set<String> set = new HashSet<String>();
	for (String keyword : competitors) {
		set.add(keyword);
	}

	Map<String, Integer> count = new HashMap();
		for (String review : reviews) {
		  Set<String> used = new HashSet<String>();
			// splits whenever there is a non-alpha numeric character
		String[] words=review.toLowerCase().split("\\W");
			for (String word : words) {
			if (set.contains(word) && 		!used.contains(word)) {
					count.put(word, count.getOrDefault(word, 0) + 1);
					used.add(word);
				}
			}
		}

		// Max heap for lexicographical order
		Queue<String> queue = new PriorityQueue<String>(
				(a, b) -> (count.get(a) == count.get(b) ? b.compareTo(a) : count.get(a) - count.get(b)));

		// keeping heap size of  topNCompetitorsfor (String s : count.keySet()) {
			queue.add(s);
			if (queue.size() > topNCompetitors) {
				queue.poll();
			}
		}

		while (!queue.isEmpty()) {
			res.add(queue.poll());
		}

		Collections.reverse(res);
		return res;
	}

27 views