Deshaw Inc Interview Question


Country: India
Interview Type: In-Person




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

public class SubSequence {
    public enum type {
        MAX, MIN
    }

    public static void main(String[] args) {
//        int[] arr = {-9, -9, 9, 4, 4, -2, 1, -2, 4, 6, -3, 5, 7, -2, -3};
        int[] arr = {3, 4, -2, 1, -2, 4, 6, -3, 5};
        int m = 2;
        System.out.println("Max :" + findSubSequence(m, arr, type.MAX));
        System.out.println("Min :" + findSubSequence(m, arr, type.MIN));
    }

    private static int findSubSequence(int m, int[] arr, type type) {
        int calculatedValue;
        if (type == type.MAX) {
            calculatedValue = Integer.MIN_VALUE;
        } else {
            calculatedValue = Integer.MAX_VALUE;
        }

        for (int i = 0; i < arr.length; ++i) {
            if (type == type.MAX) {
                calculatedValue = Math.max(subSequence(m + 1, arr, i, type.MAX), calculatedValue);
            } else {
                calculatedValue = Math.min(subSequence(m + 1, arr, i, type.MIN), calculatedValue);
            }
        }
        return calculatedValue;
    }

    private static int subSequence(int m, int[] arr, int index, type type) {
        int calculatedValue;
        if (type == type.MAX) {
            calculatedValue = Integer.MIN_VALUE;
        } else {
            calculatedValue = Integer.MAX_VALUE;
        }
        if (index > (arr.length - 1)) {
            return 0;
        }
        if ((type == type.MAX && arr[index] < 0) || (arr[index] > 0 && type == type.MIN) )
            return 0;
        int tmp = index;
        for (; index < tmp + m + m; ++index) {
            if (type == type.MAX) {
                calculatedValue = Math.max(subSequence(m, arr, index + m, type), calculatedValue);
            }else {
                calculatedValue = Math.min(subSequence(m, arr, index + m, type), calculatedValue);
            }
        }
        return calculatedValue + arr[tmp];
    }
}

- Arun Kumar Gupta August 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// code for finding max sum :

#include<iostream>
using namespace std;
int max(int a,int b)
{
return (a>b ? a :b);
}
int func(int arr[],int n,int m)
{
int maxsum;
int incl[n];
int excl[n];
incl[0] = arr[0];
excl [0] = 0;
for(int i=1;i<n;i++)
{
if(i-m-1 >=0)
{
incl[i] = max(incl[i-m-1],excl[i-m-1]) + arr[i];
}
else
incl[i] = arr[i];
excl[i] = max(excl[i-1],incl[i-1]);
maxsum = max(maxsum,max(incl[i],excl[i]));
}

return maxsum;
}
int main(){

int arr[] = {13,4,-2,1,-2,8,6,-3,5};
int n = sizeof(arr)/sizeof(arr[0]);
int m = 2;
cout<< func(arr,n,m);

}

- Mahak November 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public class FindSequence {

public static void main(String []args){
int []arr={-9, -9, 9,4, 4, -2, 1, -2, 4, 6, -3, 5,7,-2,-3};
int m=2;
int max=0;
int min=Integer.MAX_VALUE;
int len=2;
int minStartIndex=0;
int maxStartindex=0;

for(int i=0; i<arr.length;i++){
int currentSum=0;
if((i+len)<=arr.length){
for(int j=i;j<i+len;j++){
currentSum=currentSum+arr[j];
}
}
if(max<currentSum){
max=currentSum;
maxStartindex=i;
}else if(min>currentSum){
min=currentSum;
minStartIndex=i;
}
}

System.out.println("Max: "+max+", Max Index: "+maxStartindex);
System.out.println("Min: "+min+", Min Index: "+minStartIndex);
}

}

- Ramkrishna July 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public class FindSequence {

public static void main(String []args){
int []arr={-9, -9, 9,4, 4, -2, 1, -2, 4, 6, -3, 5,7,-2,-3};
int m=2;
int max=0;
int min=Integer.MAX_VALUE;
int len=2;
int minStartIndex=0;
int maxStartindex=0;

for(int i=0; i<arr.length;i++){
int currentSum=0;
if((i+len)<=arr.length){
for(int j=i;j<i+len;j++){
currentSum=currentSum+arr[j];
}
}
if(max<currentSum){
max=currentSum;
maxStartindex=i;
}else if(min>currentSum){
min=currentSum;
minStartIndex=i;
}
}

System.out.println("Max: "+max+", Max Index: "+maxStartindex);
System.out.println("Min: "+min+", Min Index: "+minStartIndex);
}

}

- Ramkrishna July 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

{{public class FindSequence {

public static void main(String []args){
int []arr={-9, -9, 9,4, 4, -2, 1, -2, 4, 6, -3, 5,7,-2,-3};
int m=2;
int max=0;
int min=Integer.MAX_VALUE;
int len=2;
int minStartIndex=0;
int maxStartindex=0;

for(int i=0; i<arr.length;i++){
int currentSum=0;
if((i+len)<=arr.length){
for(int j=i;j<i+len;j++){
currentSum=currentSum+arr[j];
}
}
if(max<currentSum){
max=currentSum;
maxStartindex=i;
}else if(min>currentSum){
min=currentSum;
minStartIndex=i;
}
}

System.out.println("Max: "+max+", Max Index: "+maxStartindex);
System.out.println("Min: "+min+", Min Index: "+minStartIndex);
}

}}}

- Ramkrishna July 26, 2016 | 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