PDF Download
AMAZON ONLINE ASSESMENT EXAM QUESTIONS
Actual Qs and Ans Expert-Verified Explanation
This Exam contains:
-Guarantee passing score -22 Questions and Answers -format set of multiple-choice -Expert-Verified Explanation
Question 1: public String mostCommonWord(String paragraph, String[] banned) {
String[] words = paragraph.replaceAll("[^a-zA-Z ]", "").split("\s+"); Set
Answer:
Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn't banned, and that the answer is unique.Words in the list of banned words are given in lowercase, and free of punctuation. Words in the
paragraph are not case sensitive. The answer is in lowercase.
Example:
Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit." banned = ["hit"]
Output: "ball"
Explanation: "hit" occurs 3 times, but it is a banned word. "ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. Note that words in the paragraph are not case sensitive, that punctuation is ignored (even if adjacent to words, such as "ball,"), and that "hit" isn't the answer even though it occurs more because it is banned.
Question 2: double max = -1; // have a global max public double
maximumAverageSubtree(TreeNode root) { helper(root); return max; // return the calculated global maximum avg } public double[] helper(TreeNode root) {
if (root == null) return null; // base case: node is null
double sum = root.val; double numNode = 1; // from valid child we receive {average value, number of nodes} // add values to current sum and numNode double[] left = helper(root.left); double[] right = helper(root.right); if (left != null) { sum += left[0] * left[1]; numNode += left[1]; } if (right != null) { sum += right[0] * right[1]; numNode += right[1]; } // calculate the local average and replace the global if bigger double local = sum / numNode; if (local > max) { max = local; } // return {current average, number of nodes} return new double[] {local, numNode}; }
Answer:
Given the root of a binary tree, find the maximum average value of any subtree of that tree.(A subtree of a tree is any node of that tree plus all its descendants. The average value of a tree is the sum of its values, divided by the number of nodes.)
Input:
[5,6,1]
Output:
6.00000
Explanation:
For the node with value = 5 we have an average of (5 + 6 +
1) / 3 =
4.For the node with value = 6 we have an average of 6 / 1 = 6.For the node with value = 1 we have an average of 1 / 1 = 1.So the answer is 6 which is the maximum.
Question 3: class Point {
int x, y; public Point(int x, int y) { this.x = x; this.y = y; } } public int orangesRotting(int[][] grid) { Queue
grid[row - 1][col] = 2; queue.add(new Point(row - 1, col)); } if(isFresh(grid, row, col + 1)) { fresh--; if(fresh == 0) return minute; grid[row][col + 1] = 2; queue.add(new Point(row, col + 1)); } if(isFresh(grid, row, col - 1)) { fresh--; if(fresh == 0) return minute; grid[row][col - 1] = 2; queue.add(new Point(row, col - 1)); } size--; } minute++; } return -1; } private boolean isFresh(int[][] grid, int row, int col) { if(row < 0 || row >= grid.length || col < 0 || col >= grid[0].length || grid[row][col] != 1) return false; return true; }
Answer:
- Rotting Oranges
Given a 2D grid, each cell is either a zombie 1 or a human 0. Zombies can turn adjacent (up/down/left/right) human beings into zombies every hour. Find out how many hours does it take to infect all humans?
Example:
Input: [
[0, 1, 1, 0, 1],
[0, 1, 0, 1, 0],
[0, 0, 0, 0, 1],
[0, 1, 0, 0, 0]
]
Output: 2
Explanation: At the end of the 1st hour, the status of the grid: [