anom
BAN USER- 0of 0 votes
AnswersSuppose you are given a puzzle that is represented as a matrix with 0s and 1s, where a 0 indicates you’re allowed to move into that position and 1 means you’re not allowed to move in that position. Write a function that given a start position and an end position, returns a boolean value indicating if there exists a path from start to end. you are only allowed to move up, left, right and down. Diagonal movement is not allowed.
- anom in United States
Example #1
Input
0 0 1 0 1
0 0 0 0 0
0 1 1 1 1
0 1 1 0 0
start: 4,1
end 0,3
Output - true
Example #2
Input
0 0 1 1 1
0 1 0 0 0
1 1 1 1 1
0 0 0 0 1
start: 0,0
end: 1,2
Output - false| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Java - 1of 1 vote
AnswersImplement a function that returns the i-th most popular item sold
- anom in United States
at xyz company. You cannot rely on any libraries.
Class Item {
String itemId;
int quantitySold;
}
/**
find the i-th most popular item in the list
**/
String find(List<Item> items, int i) {
// your code goes here
}| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Java - 0of 0 votes
AnswersSuppose you are given a puzzle that is represented as a matrix with 0s and 1s, where a 0 indicates you’re allowed to move into that position and 1 means you’re not allowed to move in that position. Write a function that given a start position and an end position, returns a boolean value indicating if there exists a path from start to end. you are only allowed to move up, left, right and down. Diagonal movement is not allowed.
- anom in United States
Example #1
Input
0 0 1 0 1
0 0 0 0 0
0 1 1 1 1
0 1 1 0 0
start: 4,1
end 0,3
Output - true
Example #2
Input
0 0 1 1 1
0 1 0 0 0
1 1 1 1 1
0 0 0 0 1
start: 0,0
end: 1,2
Output - false| Report Duplicate | Flag | PURGE
Amazon Software Engineer
public class Test {
private static int[] mergedArr;
public static void main(String[] args) {
int[] arr1 = {1,3,5,7};
int[] arr2 = {3,6,8,9};
mergedArr = new int[arr1.length + arr2.length];
mergeArray(arr1, arr2);
for(int i : mergedArr) {
System.out.println(i);
}
}
public static void mergeArray(int[] left, int[] right) {
int i=0;
int j=0;
int k=0;
while(i < left.length && j < right.length)
{
if(left[i] < right[j])
{
mergedArr[k++] = left[i++];
}
else
{
mergedArr[k++] = right[j++];
}
}
while(i < left.length) {
mergedArr[k++] = left[i++];
}
while(j < right.length) {
mergedArr[k++] = right[j++];
}
}
}
public class CompareList {
public static void main(String[] args) {
String[] unsortList = {"god","dog", "abc", "cab", "man"};
//before sort
System.out.println("ArrayList is unsort");
for(String temp: unsortList){
System.out.println(temp);
}
for(int i=0; i<unsortList.length; i++) {
for(int j=1; j<unsortList.length-i; j++) {
if(unsortList[j-1].compareTo(unsortList[j]) > 0) {
String temp = unsortList[j-1];
unsortList[j-1] = unsortList[j];
unsortList[j] = temp;
}
}
}
//after sorted
System.out.println("ArrayList is sorted");
for(String temp: unsortList){
System.out.println(temp);
}
}
}
public class MatrixClass {
public static void main(String[] args) {
int[][] arr = new int[][] {
{1,1,0,1},
{1,1,1,0},
{1,1,0,0},
{1,1,0,0}
};
checkMatrix(arr);
}
public static void checkMatrix(int[][] matrix) {
int row = matrix.length;
int col = matrix[0].length;
int count_1s = 0;
int count_0s = 0;
for(int i=0; i<row; i++)
{
int temp = matrix[i][0];
int count=0;
for(int j=0; j<col; j++) {
if(temp == matrix[i][j]) {
count++;
}
}
if(count == col && temp == 1) {
count_1s++;
} else if(count == col && temp == 0) {
count_0s++;
}
}
for(int i=0; i<col; i++)
{
int temp = matrix[0][i];
int count=0;
for(int j=0; j<row; j++) {
if(temp == matrix[j][i]) {
count++;
}
}
if(count == row && temp == 1) {
count_1s++;
} else if(count == row && temp == 0) {
count_0s++;
}
}
System.out.println("O's: " + count_0s + " , 1's: " + count_1s);
}
}
public class Combination {
public static void main(String[] args) {
String str = "abcde";
String tmp="";
System.out.println("Print all combinations: ");
for(int i=0; i<str.length(); ++i) {
tmp += str.charAt(i);
printCombiantion(tmp, str.substring(i+1, str.length()), String.valueOf(str.charAt(i)));
}
}
public static void printCombiantion(String s, String t, String indi) {
System.out.println(indi);
for(int i=0; i<t.length(); i++) {
System.out.println(s+t.charAt(i));
}
}
}
- anom June 09, 2015