Saturday, October 11, 2014

luckySum

Ques:
Given 3 int values, a b c, return their sum. However, if one of the values is 13 then it does not count towards the sum and values to its right do not count. So for example, if b is 13, then both b and c do not count.

Ans:
public int luckySum(int a, int b, int c) {
       if(a==13){
          a=0;
          b=0;
          c=0;
      }
        if(b==13){
            b=0;
            c=0;
        }
        if(c==13){
            c=0;
        }
        return a+b+c;
}

Sunday, October 5, 2014

notAlone

Ques:We'll say that an element in an array is "alone" if there are values before and after it, and those values are different from it. Return a version of the given array where every instance of the given value which is alone is replaced by whichever value to its left or right is larger.

Ans:
public int[] notAlone(int[] nums, int val) {
         int swap = 0;
        for (int i = 0; i < nums.length-1; i++) {
            if ((nums[i] == val&&i!=0)&&(nums[i] == val&&i!=nums.length-1)){
              //check the vale before and after it
                if ((nums[i-1] > nums[i + 1])) {
                    nums[i] = nums[i - 1];
                } else if (nums[i-1] < nums[i + 1]) {
                    nums[i] = nums[i + 1];
                }
            }
        }
 
        return nums;
    }


Caught Speeding

Ques:You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.


Ans:
public int caughtSpeeding(int speed, boolean isBirthday) {
           int val = 0;
        if (isBirthday) {
            if (speed <= 65) {
                val = 0;
            }
            if (speed > 65 && speed <= 85) {
                val = 1;
            }
            if (speed > 85) {
                val = 2;
            }
        }
       
        if(!isBirthday){
            if (speed <= 60) {
                val = 0;
            }
            if (speed > 60 && speed <= 80) {
                val = 1;
            }
            if (speed > 80) {
                val = 2;
            }
        }
        return val;

squirrelPlay

Ques:The squirrels in Palo Alto spend most of the day playing. In particular, they play if the temperature is between 60 and 90 (inclusive). Unless it is summer, then the upper limit is 100 instead of 90. Given an int temperature and a boolean isSummer, return true if the squirrels play and false otherwise.

Ans: public static boolean squirrelPlay(int temp, boolean isSummer) {
        boolean val = false;
        if (isSummer&& (temp >= 60 && temp <= 100)) {
                val = true;
            } else if (!isSummer && (temp >= 60 && temp <= 90)) {
                val = true;
            } else {
                val = false;
            }
       
        return val;
    }

dateFashion

Ques:You and your date are trying to get a table at a restaurant. The parameter "you" is the stylishness of your clothes, in the range 0..10, and "date" is the stylishness of your date's clothes. The result getting the table is encoded as an int value with 0=no, 1=maybe, 2=yes. If either of you is very stylish, 8 or more, then the result is 2 (yes). With the exception that if either of you has style of 2 or less, then the result is 0 (no). Otherwise the result is 1 (maybe).

Ans:
       int val = 0;
        if((you<=2||date<=2)){
            val=0;
        }else if((you>2&&you<8)&&(date>2&&date<8)){
            val=1;
        }else{
            val=2;
        }
        return val;

Cigar Party

Ques:
When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of cigars. Return true if the party with the given values is successful, or false otherwise.

Ans:
 public static boolean cigarParty(int cigars, boolean isWeekend) {
       
        return isWeekend&&(cigars>=40)?true:!isWeekend&&(cigars>=40&&cigars<=60?true:false);
    }

Saturday, October 4, 2014

tenRun

Ques:For each multiple of 10 in the given array, change all the values following it to be that multiple of 10, until encountering another multiple of 10. So {2, 10, 3, 4, 20, 5} yields {2, 10, 10, 10, 20, 20}.

Ans:
        for(int i=0;i<nums.length;i++){
            if(nums[i]%10==0){
               for(int j=i+1;j<nums.length;j++){
                   if(nums[j]%10!=0){
                       nums[j]=(nums[i]/10)*10;
                      
                   }else break;
               }
             }
        }
        return nums;
    }