## Interview Question

Country: United States

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````private int findMaxWeapons(int x, int y, int z) throws Exception {
if (x > y) {
return x - z * (x / y);
} else if (x <= y) {
return x - z;
} else {
return -1;
}
}``````

Comment hidden because of low score. Click to expand.
0

x=3000
y = 1000
z = 1000

output is 833

but with code above it is zero

Comment hidden because of low score. Click to expand.
0

If y = 1000 and z = 1000, then even a fully loaded truck would lose all the weapons before reaching its destination, so the output should be 0.

The code above, however, does not account for a possible not fully loaded last trip. Example:
x = 3500
y = 1000
z = 100

Output should be 3100 but the code will output 2700

Comment hidden because of low score. Click to expand.
0

for 3000, 1000, 1000. First fill 1000 in truck move to 500 and drop 500 there repeat 3 times. Now problem reduced to X= 1500 y = 1000, Z = 500 now answer will be 750. But can't satisfy 833

Comment hidden because of low score. Click to expand.
0
of 0 vote

void f(){
dp=w;
for(int i=0;i<=d;i++){
for ( int j = i+1; j <=d;j++){
int x = (dp[i]/c)*(j-i),y = (dp[i]%c>=j-i)? (j-i) : dp[i]%c ;
dp[j] = max(dp[j],dp[i]-x-y );
}
}
cout<<dp[d];
}

Comment hidden because of low score. Click to expand.
0

Comment hidden because of low score. Click to expand.
0

the idea is similar to Longest increasing subsequence problem, you have to check through all possible combinations, and store the most optimal solution.
dp[i] stores the max no of weapons which can be transported from 0 to i.
x, y stores the weapons lost when transporting from i to j.

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public class MaxWeapons {

private static int getMaxWeapons(int x, int y, int z) {
if (x < y) {
return Math.max(x - z, 0);
}
int count = x;
for (int i = 1; i <= z; i++) {
count = count - (int) Math.ceil(((double) count) / y);
if (count <= 0) {
break;
}
}
return Math.max(0, count);
}

public static void main(String[] args) {
System.out.println(getMaxWeapons(3000, 1000, 1000));
System.out.println(getMaxWeapons(1000, 1200, 1));
System.out.println(getMaxWeapons(1000, 1200, 100));
}

}``````

To get maximum weapons to the destination we can move all the weapons to one KM and then again full load the truck and carry. So we need to keep doing unloading/loading at 1 KM of distance.

Comment hidden because of low score. Click to expand.
0

what if X, Y, Z are too large?

Comment hidden because of low score. Click to expand.
0
of 0 vote

Obvious linear time solution, not sure how to compute it faster:

``````x = 15
y = 3
z = 2

for i in range(z):
if x == 0:
break
x = x - (x+y-1)//y
print (x)``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public static long findMaxWeapons(long X, long Y, long Z) {
if(X <= Y)
return X - Z;
long wh;
if( X % Y == 0)
{
if(Y % (X / Y) == 0)
wh = Y / (X/Y);
else
wh = Y / (X/Y) + 1;
}
else
{
if((X % Y) % (X / Y + 1) == 0)
wh = (X % Y)/(X / Y + 1);
else
wh = (X % Y)/(X / Y + 1) + 1;
}
if(wh >= Z)
{
if(X % Y == 0)
return X - X * Z/ Y;
else
{
long rem = X % Y;
long ini = X - X * Z / Y;
if(rem > Z)
ini = ini + rem - Z;
return ini;
}
}
else
return findMaxWeapons(X - (((X % Y) == 0)? X/Y : X/Y + 1) * wh , Y , Z - wh);``````

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

public static long findMaxWeapons(long X, long Y, long Z) {
if(X <= Y)
return X - Z;
long wh;
if( X % Y == 0)
{
if(Y % (X / Y) == 0)
wh = Y / (X/Y);
else
wh = Y / (X/Y) + 1;
}
else
{
if((X % Y) % (X / Y + 1) == 0)
wh = (X % Y)/(X / Y + 1);
else
wh = (X % Y)/(X / Y + 1) + 1;
}
if(wh >= Z)
{
if(X % Y == 0)
return X - X * Z/ Y;
else
{
long rem = X % Y;
long ini = X - X * Z / Y;
if(rem > Z)
ini = ini + rem - Z;
return ini;
}
}
else
return findMaxWeapons(X - (((X % Y) == 0)? X/Y : X/Y + 1) * wh , Y , Z - wh);
}

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.