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;
        }
    }

- AEK September 07, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

x=3000
y = 1000
z = 1000

output is 833

but with code above it is zero

- anoophky September 07, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous September 07, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- anoophky September 09, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void f(){
dp[0]=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];
}

- chandankumar80567 September 08, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

explain your code.

- anoophky September 09, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- chandankumar80567 September 09, 2019 | Flag
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.

- xyz September 16, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if X, Y, Z are too large?

- Anonymous October 30, 2019 | Flag
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)

- adr September 17, 2019 | Flag Reply
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);

}

- Jaa Yuksh October 31, 2019 | Flag Reply
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);
}

- Anonymous October 31, 2019 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More