Amazon New Grad 2021 OA1 Debug

Amazon New Grad 2021 OA1 Debug

    • checkPairSumExists
    • removeConsecutiveVowels
    • reverseAlphabetCharsOnly
    • calculateSumOfNumbersInString
    • countTripletSumPermutations
    • compareProduct
    • countRotations

20min 7道debug题

Code Debugging requires you to identify and address compilation, logical, and syntactical errors and takes approximately 20 minutes to complete. You will have the choice of viewing code in Java (Java 1.8), C++ (GCC 8.1.0), or C (GCC 8.1.0).

Amazon OA Demo

  • You may use publicly accessible online resources (e.g., the JDK or STL), but please do not access sites that require a login or are private repositories. Browser usage is logged.
  • Do not leverage the print screen functionality as your session will be terminated, and you will be locked out. Also, avoid using copy/paste in the assessment.

Amazon Online Assessment Debugging 2021 Compiling
Amazon Online Assessment 1 (Another 7 questions)
2020 new grad OA1+2+3
Amazon OA 技巧


checkPairSumExists

ine19: sum 改成 arr[j].

public class Solution {
    public boolean checkPairSumExists(int rows, int cols, int[][] arr, int sum) {
        Set<Integer> set = new HashSet<Integer>();
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {
                if(set.contains(sum - arr[i][j])) {
                    return true;
                } else {
                	// set.add(sum) 更改!!!!!!!!!!!
                    set.add(arr[i][j]);
                }
            }
        }
        return false;
    }
}

removeConsecutiveVowels

line20: && 更改为 ||.

public class Solution {
    boolean is_vowel(char ch) {
        return (ch == 'a') || (ch == 'e') ||
                (ch == 'i') || (ch == 'o') ||
                (ch == 'u');
    }

    public String removeConsecutiveVowels(String str) {
        String str1 = "";
        str1 = str1+str.charAt(0);
        for(int i = 1; i < str.length(); i++)
            if((!is_vowel(str.charAt(i - 1))) || // && 更改!!!!!!!!
                    (!is_vowel(str.charAt(i)))) {
                char ch = str.charAt(i);
                str1 = str1 + ch;
            }
        return str1;
    }
}

reverseAlphabetCharsOnly

line 20&21 放进15-19 的大括号里.

public class Solution {
    public String reverseAlphabetCharsOnly(String inputString) {
        char[] inputChar = inputString.toCharArray();
        int right = inputString.length() - 1;
        int left = 0;
        while(left < right) {
            if(!Character.isAlphabetic(inputChar[left]))
                left++;
            else if(!Character.isAlphabetic(inputChar[right]))
                right--;
            else {
                char temp = inputChar[left];
                inputChar[left] = inputChar[right];
                inputChar[right] = temp;
                left++;	// 添加!!!!!!!!
            	right--;
            }
            // left++; 移到上面
            // right--;
        }
        return new String(inputChar);
    }
}

calculateSumOfNumbersInString

line14 放进else里面.

public class Solution {
    public int calculateSumOfNumbersInString(String inputString) {
        String temp = "";
        int sum = 0;
        for(int i = 0; i < inputString.length(); i++) {
            char ch = inputString.charAt(i);
            if(Character.isDigit(ch))
                temp += ch;
            else{
                sum += Integer.parseInt(temp);
            	temp = "0";	// 放入else里面!!!!!!!!!!!!!!
            	}
        }
        return sum + Integer.parseInt(temp);
    }
}

countTripletSumPermutations

line10: int j = 0 改成 int j = i + 1.

public class Solution {
    public int countTripletSumPermutations(int size, int[] arr, int tripletSum)
    {
        int count = 0;
        for(int i = 0; i < size - 2; i++)
        {
            if(tripletSum % arr[i] == 0)
            {
            	for(int j = i+1; j < size - 1; j++)	// int j = 0 改成 j = i+1  !!!!!!!!!!!!!
                // for(int j = 0; j < size - 1; j++)
                {
                    if(tripletSum % (arr[i] * arr[j]) == 0)
                    {
                        int value = tripletSum / (arr[i] * arr[j]);
                        for(int k = j + 1; k < size; k++)
                            if(arr[k] == value)
                                count++;
                    }
                }
            }

        }
        return count;
    }
}

compareProduct

line11&16: num / 10 => num % 10.

public class Solution {
    public boolean compareProduct(int num) {
        if (num < 10)
            return false;
        int oddProdValue = 1, evenProdValue = 1;

        while(num > 0) {
            int digit = num % 10;  //num / 10; 改!!!!!!!
            oddProdValue *= digit;
            num = num / 10;
            if(num == 0)
                break;
            digit = num % 10; 	// num / 10; 改!!!!!!!!!
            evenProdValue *= digit;
            num = num / 10;
        }
        if(evenProdValue == oddProdValue)
            return true;
        return false;
    }
}

countRotations

line21 mid - 1 => mid.

public int countRotationsUtil(int list[], int low, int high)
{
	if (high < low)
		return 0;
		
	if (high == low)
		return low;
	
	int mid = low + (high - low) / 2;

	if (mid < high && list[mid + 1] < list[mid])
		return (mid + 1);
	
	// if (mid > low && list[mid] < list[mid - 1])	更改!!!!
	if (mid > low && list[mid] < list[mid])
		return mid;

	if (list[high] > list[mid])
		return countRotationsUtil(list, low, mid);
	
	return countRotationsUtil(list, mid, high);
}

你可能感兴趣的:(面经,笔试/面试准备,面试)