Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
a short java demo, you can also add parseInt to get the int or just leave it as string
public static int maxV (int [] arr) throws Exception{
int max = Integer.MIN_VALUE;
for (int i = 0 ; i < arr.length; i++){
if (arr[i] > max){
max = arr[i];
}
}
throw new Exception (""+max);
}
public static void main (String args[]){
try {
int [] arr = {1,45,1,12,4};
maxV (arr);
} catch (Exception e){
System.out.println(e.getMessage());
}
}
a short java demo, you can also add parseInt to get the int or just leave it as string
public static int maxV (int [] arr) throws Exception{
int max = Integer.MIN_VALUE;
for (int i = 0 ; i < arr.length; i++){
if (arr[i] > max){
max = arr[i];
}
}
throw new Exception (""+max);
}
public static void main (String args[]){
try {
int [] arr = {1,45,1,12,4};
maxV (arr);
} catch (Exception e){
System.out.println(e.getMessage());
}
}
Well this is just a toy code that assumes we already know the array size. We can start with this and then enhance it.
- Nitin March 13, 2012