Adobe Interview Question for Developer Program Engineers


Country: India




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

void function(int arr[], int size)
{
    int maxGlobal = arr[0];
    int maxCurrent = arr[0];
    
    int start = 0, end = 0, prevEnd = 0, prevMax = 0;
    
    for(int i=1;i<=size;i++)
    {
        if(arr[i] > arr[i]+maxCurrent)
        {
            start = i;
            end = i;
            maxCurrent = arr[i];
        }
        else
        {
            maxCurrent = arr[i]+maxCurrent;
            end++;
            if(prevMax<maxCurrent)
            {
                prevMax = maxCurrent;
                prevEnd = end;
            }
        }
        
        maxGlobal = max(maxCurrent, maxGlobal);
    }

    cout<<"  Maximum sum :- "<<maxGlobal<<"   start = "<<start<<"  prevEnd = "<<prevEnd<<endl;
}

- skum July 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void getFirstAndLast(int[] nums){
		int maxSoFar = nums[0];
		int temp = 0, last = 0, first = 0;
		int currentMax = nums[0];
		for (int i = 1; i < nums.length; i++) {
			if(currentMax + nums[i] < currentMax){
				temp = i;
				currentMax = nums[i];
			} else{
				currentMax = currentMax + nums[i];
			}
			if(currentMax > maxSoFar){
				last = i;
				first = temp;
				maxSoFar = currentMax;
			}
		}
		System.out.println("First: "+first +" Last: "+last);
	}

- deepak.bitp July 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java impl

public class MaxContiguousSum {

	static class ValueObject{
		protected final int max_so_far,start,end;
		
		public ValueObject(int max_so_far, int start, int end){
			this.max_so_far=max_so_far;
			this.start=start;
			this.end=end;
		}
		
		public String toString(){
			return "{max_so_far: "+max_so_far+", start: "+start+", end: "+end+"}";
		}
	}
	
	public static ValueObject maxSubArraySum(int a[]){
		int max_so_far=a[0], i, start_curr=0, end_curr=0;
		int curr_max=a[0];
		
		for(i=1; i < a.length; i++){
			if(a[i] > curr_max+a[i]){
				start_curr=i;
				end_curr=i;
			}
			
			curr_max = Math.max(a[i], curr_max+a[i]);
			
			max_so_far = Math.max(max_so_far,curr_max);
			end_curr++;
		}
		
		return new ValueObject(max_so_far, start_curr, end_curr);
	}
	
	public static void main(String[] args) {
		int array[]={1,2,3,-4,-2,-6,8,-1,-3};
		System.out.println("Result: "+maxSubArraySum(array));
	}

}

Output:
Result: {max_so_far: 8, start: 6, end: 9}

- Yev July 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Fix:

public static ValueObject maxSubArraySum(int a[]){
		int max_so_far=a[0], i, start_curr=0, end_curr=0;
		int curr_max=a[0];
		
		for(i=1; i < a.length; i++){
			if(a[i] > curr_max+a[i]){
				start_curr=i;
				end_curr=i;
			}
			
			curr_max = Math.max(a[i], curr_max+a[i]);
			
			max_so_far = Math.max(max_so_far,curr_max);
			end_curr++;
		}
		
		return new ValueObject(max_so_far, start_curr, end_curr-1);
	}

Output:
Result: {max_so_far: 8, start: 6, end: 8}

- Yev July 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Fixed to cover use-case scenarios and base-cases. I think that's the really tricky part of this problem, more than the coding itself.

public class MaxContiguousSum {

	static class ValueObject{
		protected final int max_so_far,start,end;
		
		public ValueObject(int max_so_far, int start, int end){
			this.max_so_far=max_so_far;
			this.start=start;
			this.end=end;
		}
		
		public String toString(){
			return "{max_so_far: "+max_so_far+", start: "+start+", end: "+end+"}";
		}
	}
	

	
	public static ValueObject maxSubArraySum(int a[]){
		if(null==a || a.length==0){
			return null;
		}
		else if(a.length==1){
			return new ValueObject(a[0], 0, 0);
		}
		
		int max_so_far=a[0], i, start_curr=0, end_curr=0, start_prev=0, end_prev=0;
		int curr_max=a[0];
		
		//5,-1,7
		for(i=1; i < a.length; i++){
			// contiguous monotonically-increasing sum
			if(curr_max + a[i] >= curr_max){
				curr_max += a[i];
				end_curr=i;
			}
			
			// If monotonically decreasing and next element is greater, then skip to next element
			// If curr_max decreased, then skip to next element
			if(curr_max+a[i] < a[i] || curr_max + a[i] < curr_max){
				start_curr=i;
				end_curr=i;
				curr_max=a[i];
			}
			
			// Current sum is greater than max-so-far, then save indices of max-so-far
			if(curr_max > max_so_far){
				start_prev=start_curr;
				end_prev=end_curr;
				max_so_far=curr_max;
			}
		}
		
		return new ValueObject(max_so_far, start_prev, end_prev);
	}
	
	public static void main(String[] args) {
		int array[]={1,2,3,-4,-2,-6,8,-1,-3};
		System.out.println("Result: "+maxSubArraySum(array));
	}

}

Output:
Result: {max_so_far: 6, start: 0, end: 2}

- Yev July 12, 2015 | Flag


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