Amazon Interview Question
Developer Program EngineersCountry: India
Interview Type: In-Person
import java.util.Scanner;
public class ArrayFrequency{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int temp=0;
int n=0;
int[] array;
while (sc.hasNextInt()) {
n = sc.nextInt();
if(n<0 && n>10 ){
System.out.println("please enter a valid numer.");
break;
}
temp=temp+n;
array = new int[n];
for(int i=n-1;i>=0;i--){
array[i]=n;
}
for(int x: array){
System.out.println(x);
}
}
if(sc.hasNextInt()==false){
sc.close();
System.out.println("plese enter a valid numer from 0-9.Exit...");
Runtime.getRuntime().exit(0);
}
}
package com.demo;
import java.util.Scanner;
public class ArrayFrequency{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int temp=0;
int count=0;
System.out.println("please enter a no of elements.");
int number = sc.nextInt();
int[] array=new int[number];
for(int i=0;i<number;i++){
array[i]=0;
}
int n ;
while(count<number){
System.out.println("Enter element->.");
n = sc.nextInt();
if(n<0 && n>10 ){
System.out.println("please enter a valid numer.");
break;
}
array[n]++;
count++;
}
for(int i=0;i<number;i++){
System.out.print(array[i]);
System.out.print("");
}
}
}
Seems like a very easy problem, for an in-person interview.
Unless I'm missing something.
public static int[] getCnt(){
Scanner in = new Scanner(System.in)
int[] res = new int[10];
for(in.hasNextInt()){
int t = in.nextInt();
if(t >= 0 && t <= 9){
res[t] += 1;
}
else{
throw new IllegalArgumentException("Please enter a number between 0-9");
}
}
return res;
}
The way the question is stated it doesn't make any sense.
- Paul March 21, 2017