Kony India Private Limited Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

I wish we had some input/output examples, but I believe dynamic programming can solve this.

function maximize(v, index) {
  if (index >= v.length) { 
    return 0; 
  }else {
    return Math.max(
      v[index] + maximize(v, index+2), //include current
      maximize(v, index+1) //exclude current
    );
  }
}

To improve, we can add a cache map to avoid recalculation.

- tnutty2k8 September 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class MaxTheft {

public static void main(String[] args) {

System.out.println("{6, 8, 1, 7, 2}" + "\t:\t" + maxTheft(new double[]{6, 8, 1, 7, 2}) + "\t:\t" + bruteForce(new double[]{6, 8, 1, 7, 2}, 0));
System.out.println("{1, 8, 1, 1, 4, 1}" + "\t:\t" + maxTheft(new double[]{1, 8, 1, 1, 4, 1}) + "\t:\t" + bruteForce(new double[]{1, 8, 1, 1, 4, 1}, 0));
System.out.println("{6, 3, 1, 7, 12}" + "\t:\t" + maxTheft(new double[]{6, 3, 1, 7, 12}) + "\t:\t" + bruteForce(new double[]{6, 3, 1, 7, 12}, 0));
System.out.println("{9, 8, 1, 2, 7}" + "\t:\t" + maxTheft(new double[]{9, 8, 1, 2, 7}) + "\t:\t" + bruteForce(new double[]{9, 8, 1, 2, 7}, 0));
System.out.println("{6, 8, 1, 7, 5}" + "\t:\t" + maxTheft(new double[]{6, 8, 1, 7, 5}) + "\t:\t" + bruteForce(new double[]{6, 8, 1, 7, 5}, 0));
System.out.println("{6, 8, 1, 1, 1, 1, 1, 2, 1, 5}" + "\t:\t" + maxTheft(new double[]{6, 8, 1, 1, 1, 1, 1, 2, 1, 5}) + "\t:\t" + bruteForce(new double[]{6, 8, 1, 1, 1, 1, 1, 2, 1, 5}, 0));
}


public static double maxTheft(double[] x) {
if (x == null || x.length == 0)
return 0;

double prevWith = 0, currWith = 0, prevWithout = 0, currWithout = 0;

for (int i = x.length - 1; i >= 0; i--) {

double without = max(currWithout, prevWithout, prevWith);
double currWithout_ = max(without, currWith);

double currWith_ = x[i] + without;

prevWith = currWith;
prevWithout = currWithout;

currWith = currWith_;
currWithout = currWithout_;
}
return max(currWith, currWithout);
}

public static double max(double... array) {
double max = 0;
for (double v : array) {
max = Math.max(max, v);
}
return max;
}

static double bruteForce(double v[], int index) {
if (index >= v.length) {
return 0;
} else {
return Math.max(
v[index] + bruteForce(v, index + 2), //include current
bruteForce(v, index + 1) //exclude current
);
}
}


}

- George September 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

TC=O(n)
SC=O(1)

public class MaxTheft {

    public static void main(String[] args) {

        System.out.println("{6, 8, 1, 7, 2}" + "\t:\t" + maxTheft(new double[]{6, 8, 1, 7, 2}) + "\t:\t" + bruteForce(new double[]{6, 8, 1, 7, 2}, 0));
        System.out.println("{1, 8, 1, 1, 4, 1}" + "\t:\t" + maxTheft(new double[]{1, 8, 1, 1, 4, 1}) + "\t:\t" + bruteForce(new double[]{1, 8, 1, 1, 4, 1}, 0));
        System.out.println("{6, 3, 1, 7, 12}" + "\t:\t" + maxTheft(new double[]{6, 3, 1, 7, 12}) + "\t:\t" + bruteForce(new double[]{6, 3, 1, 7, 12}, 0));
        System.out.println("{9, 8, 1, 2, 7}" + "\t:\t" + maxTheft(new double[]{9, 8, 1, 2, 7}) + "\t:\t" + bruteForce(new double[]{9, 8, 1, 2, 7}, 0));
        System.out.println("{6, 8, 1, 7, 5}" + "\t:\t" + maxTheft(new double[]{6, 8, 1, 7, 5}) + "\t:\t" + bruteForce(new double[]{6, 8, 1, 7, 5}, 0));
        System.out.println("{6, 8, 1, 1, 1, 1, 1, 2, 1, 5}" + "\t:\t" + maxTheft(new double[]{6, 8, 1, 1, 1, 1, 1, 2, 1, 5}) + "\t:\t" + bruteForce(new double[]{6, 8, 1, 1, 1, 1, 1, 2, 1, 5}, 0));
    }


    public static double maxTheft(double[] x) {
        if (x == null || x.length == 0)
            return 0;

        double prevWith = 0, currWith = 0, prevWithout = 0, currWithout = 0;

        for (int i = x.length - 1; i >= 0; i--) {

            double without = max(currWithout, prevWithout, prevWith);
            double currWithout_ = max(without, currWith);

            double currWith_ = x[i] + without;

            prevWith = currWith;
            prevWithout = currWithout;

            currWith = currWith_;
            currWithout = currWithout_;
        }
        return max(currWith, currWithout);
    }

    public static double max(double... array) {
        double max = 0;
        for (double v : array) {
            max = Math.max(max, v);
        }
        return max;
    }

    static double bruteForce(double v[], int index) {
        if (index >= v.length) {
            return 0;
        } else {
            return Math.max(
                    v[index] + bruteForce(v, index + 2), //include current
                    bruteForce(v, index + 1) //exclude current
            );
        }
    }


}

- George September 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

package sequence;

public class MaxTheft {

    public static void main(String[] args) {

        System.out.println("{6, 8, 1, 7, 2}" + "\t:\t" + maxTheft(new double[]{6, 8, 1, 7, 2}) + "\t:\t" + bruteForce(new double[]{6, 8, 1, 7, 2}, 0));
        System.out.println("{1, 8, 1, 1, 4, 1}" + "\t:\t" + maxTheft(new double[]{1, 8, 1, 1, 4, 1}) + "\t:\t" + bruteForce(new double[]{1, 8, 1, 1, 4, 1}, 0));
        System.out.println("{6, 3, 1, 7, 12}" + "\t:\t" + maxTheft(new double[]{6, 3, 1, 7, 12}) + "\t:\t" + bruteForce(new double[]{6, 3, 1, 7, 12}, 0));
        System.out.println("{9, 8, 1, 2, 7}" + "\t:\t" + maxTheft(new double[]{9, 8, 1, 2, 7}) + "\t:\t" + bruteForce(new double[]{9, 8, 1, 2, 7}, 0));
        System.out.println("{6, 8, 1, 7, 5}" + "\t:\t" + maxTheft(new double[]{6, 8, 1, 7, 5}) + "\t:\t" + bruteForce(new double[]{6, 8, 1, 7, 5}, 0));
        System.out.println("{6, 8, 1, 1, 1, 1, 1, 2, 1, 5}" + "\t:\t" + maxTheft(new double[]{6, 8, 1, 1, 1, 1, 1, 2, 1, 5}) + "\t:\t" + bruteForce(new double[]{6, 8, 1, 1, 1, 1, 1, 2, 1, 5}, 0));
    }


    public static double maxTheft(double[] x) {
        if (x == null || x.length == 0)
            return 0;

        double prevWith = 0, currWith = 0, prevWithout = 0, currWithout = 0;

        for (int i = x.length - 1; i >= 0; i--) {

            double without = max(currWithout, prevWithout, prevWith);
            double currWithout_ = max(without, currWith);

            double currWith_ = x[i] + without;

            prevWith = currWith;
            prevWithout = currWithout;

            currWith = currWith_;
            currWithout = currWithout_;
        }
        return max(currWith, currWithout);
    }

    public static double max(double... array) {
        double max = 0;
        for (double v : array) {
            max = Math.max(max, v);
        }
        return max;
    }

    static double bruteForce(double v[], int index) {
        if (index >= v.length) {
            return 0;
        } else {
            return Math.max(
                    v[index] + bruteForce(v, index + 2), //include current
                    bruteForce(v, index + 1) //exclude current
            );
        }
    }


}

- George September 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args){
   int[] houses = {6, 8, 1, 1, 1, 1, 1, 2, 1, 5};
   System.out.println(maxValue(houses));
 }
  
  public static int maxValue(int[] houses){
  	int n = houses.length;
    
    int[] dp = new int[n];
    dp[0] = houses[0];
    dp[1] = houses[1];
    
    for(int i = 2; i < n; i++){
    	dp[i] = Math.max(dp[i-2]+houses[i], dp[i-1]);
    }
    return dp[n-1];
  }

- sudip.innovates September 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

maximum of sum of all values in odd indices and sum of all values in even indices

- Anonymous August 08, 2018 | 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