Amazon Interview Question
Quality Assurance EngineersCountry: India
Interview Type: In-Person
import java.util.Random;
import java.util.Scanner;
public class ResourceAllocationWithDice
{
static int sum= 0;
static int[] rollRec = new int[6];
static Scanner sc=new Scanner(System.in);
public static void main(String[] args)
{
for (int i = 0; i < rollRec.length; i++)
{
sum=sum+rollRec[i];
}
Scanner sc = new Scanner(System.in);
char ch;
do
{ if(sum !=21)
{
System.out.print("To roll Dice press Y/y: ");
}
else
{
System.out.print("All reasource are allocated ");
System.exit(0);
}
ch =sc.nextLine().charAt(0); ;
Random r = new Random();
int Low = 1;
int High = 7;
int diceFaceValue = r.nextInt(High-Low) + Low;
System.out.println("Dice Face Value is : "+ diceFaceValue);
allocateResource(diceFaceValue);
}
while(ch=='Y' || ch=='y');
}
private static void allocateResource(int diceFaceValue)
{
if(rollRec[diceFaceValue-1]==0)
{
rollRec[diceFaceValue-1]=diceFaceValue;
sum =sum+diceFaceValue;
if(diceFaceValue==1)
{
System.out.println("Resource is assigned to 1st host");
}
else if(diceFaceValue==2)
{
System.out.println("Resource is assigned to 2nd host");
}
else if(diceFaceValue==3)
{
System.out.println("Resource is assigned to 3rd host");
}
else
{
System.out.println("Resource is assigned to "+ diceFaceValue+"th host");
}
}
else
{
if(diceFaceValue==1)
{
System.out.println("This Resource is Allready assigned to 1st host Please Roll dice again");
}
else if(diceFaceValue==2)
{
System.out.println("This Resource is Allready assigned to 2nd host Please Roll dice again");
}
else if(diceFaceValue==3)
{
System.out.println("This Resource is Allready assigned to 3rd host Please Roll dice again");
}
else
{
System.out.println("This Resource is Allready assigned to "+ diceFaceValue +"th host Please Roll dice again");
}
}
}
}
1.First admin should check the availability of the systems. He or she can do in below manner:
- Prasenjeet September 13, 2014a. Admin should role the dice once to select a host I.e if 1 then host 1 ,2 then host 2 and so on.
b. Admins should role the dice once again to allocate one of the machine from the selected host.i.e if he has already selected a host in step a then whatever the no comes on the dice the same numbered machine, admin can allocate.
c. Now moving ahead if admin is getting a no which is already allocated then he should repeat step a and b until and unless he gets an unallocated system.