Snap Inc Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

import java.util.Map;
import java.util.HashMap;

public class SubArraySum{
	static void subArraySum(int arr[], int n, int sum){
		Map<Integer, Integer> map = new HashMap<>();
		
		int currSum = 0;
		
		for(int i=0;i<n;i++){
			System.out.println("currSum: " +currSum);
			currSum = currSum + arr[i];
			System.out.println("currSum: " +currSum);
			System.out.println("sum: " +sum);
			
			if(currSum == sum){
				System.out.println("Sum found between indexes " + 0 
					+" to " +i);
					return;
			}

			int mapKey = currSum - sum;
			System.out.println("Finding in map: " +mapKey);
			if(map.containsKey(mapKey)){
				System.out.println("Sum found between indexes " 
					+map.get(mapKey) + 1 
					+" to " +i);
					return;
			}
			System.out.println("Inserting in map: " + currSum +", " +i);
			map.put(currSum, i);
		}
	}
	
	public static void main(String args[]){
		int arr[] = {10,2,-2,-20,10};
		int n = arr.length;
		int sum = -10;
		subArraySum(arr,n,sum);
	}
}

- verma.ius April 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here you go:
[ geeksforgeeks.org/find-subarray-with-given-sum-in-array-of-integers/ ]

- NoOne April 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function sub(a, k){
  if(!Array.isArray(a) || Number(k) === NaN){
    throw new Error('Wrong input');
  }
  let map = new Map();
  let sum = 0;
  for(let i=0; i < a.length; i++){
    sum += a[i];
    let diff = sum - k;
    if(diff === k){
      return `0 to ${i}`;
    }
    let history = map.get(diff);
    if(history !== undefined){
      return `${history + 1} to ${i}`;
    }
    else{
      map.set(sum, i);
    }
  }
    return 'not found';
}
let aa = [8,5,1,4,-10,66];
console.log(sub(aa, 0));
let aa2 = [12,3,4,-500, 300, 20, 180, 40, 9];
console.log(sub(aa2, 0));

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

let   
  input = [1, 2, 3, 5, -10],
  currStart = 0,
  currEnd = 0,
  prevSum = 0,
  currSum = 0;


for(let i = 0; i < input.length; i++) {  
  currSum = input[i];
  for(let j = i + 1; j < input.length; j++) {            
    currSum += input[j];    
    if (currSum > prevSum) {
      currStart = i;
      prevSum = currSum;
      currEnd = j;
    }    
  }    
}

console.log("Array from index " + (currStart + 1) + " to index " + (currEnd + 1));

- Vladimir April 25, 2017 | 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