HRT | SWE | June 2022 [Reject]

Anonymous User
720
Jun 29, 2022
Jun 29, 2022

Status: 8 years' experience as SWE
Position: Amazon
Location: Europe

How I applied

  • Applied online using the company website. After two weeks I recived a test to do online with 3 problems (2h time).

Problems

In the online test I could only see a couple of test cases for each problem, so can't be 100% sure if my solution was perfect.

1st problem:

Done it. I think I did a perfect solution!

2nd problem:

Given an undirected graph that is represented by its adjacency matrix, return whether or not is it possible to add no more than two edges to this graph in order to make all the degrees of nodes even. Keep in mind that in the resulting graph there should be at most one edge between any pair of nodes.

graph = [[false, true, false, false],
         [true, false, true, false],
         [false, true, false, true],
         [false, false, true, false]]

Done it. My solution was perfect!

boolean addEven(boolean[][] graph) {
    int odd = 0;
    List<Integer> odd_vertices = new ArrayList<Integer>();

    for (int i = 0; i < graph.length; i++) {
        int deg = 0;
        for (int j = 0; j<graph[0].length; j++) {
            if (graph[i][j] == true) {
                deg++;
            }
        }
        if (deg % 2 == 1) {
            odd++;
            odd_vertices.add(i);
        }
    }
// Given that we have at most 2 new edges to be added the number of odd nodes should not be more than 2
    if (odd == 0) {
        return true;
    } else if (odd == 2) {
        int a = odd_vertices.get(0);
        int b = odd_vertices.get(1);
        for (int k = 0; k < graph.length; k++) {
            if (!graph[a][k] && !graph[b][k]){
                return true;
            }
        }
        return false;
    } else if (odd == 4) {
        int a = odd_vertices.get(0);
        int b = odd_vertices.get(1);
        int c = odd_vertices.get(2);
        int d = odd_vertices.get(3);
        if ((!graph[a][b] && !graph[c][d]) ||
            (!graph[a][c] && !graph[b][d]) ||
            (!graph[a][d] && !graph[b][c])) {
            
           return true;
        }
        return false;
    } else {
        return false;
}

3rd problem:

Let's consider a sequence x1 = 1;  x2= 1 ^ 2; xn = 1 ^ 2 ^ ....... ^ n; where ^ is a bitwise XOR
Given L and R find the value of: X(L) ^ X(L+1) ^ .... ^ X(R)

Example:
L=1; R=5; result = 7
L=3; R=4; result = 4

My solution was working on the test case I could see but I got the feedback that was too slow with big inputs.
Does anyone have a better algorithm for this third problem?

int solution(int l; int r) {
  int solution = 0;
  for (int i=0; i<=r; i++) {
    int subValue = i;
	if (i<l) { subValue = l;}
	if ((r-subValue+1)%2 == 1) {
		solution ^= i;
	}
  }
return solution;  
} 
Comments (2)