Flipkart Interview Question for SDE-2s


Country: India
Interview Type: Phone Interview




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

private static void displayNumbersHasSum(int[] arr,int sum) {
		HashMap<Integer,Integer> ht=new HashMap<Integer,Integer>();
		for(int i=0;i<arr.length;i++)
		{
			int k=sum-arr[i];
			if(ht.containsKey(arr[i]))
			{
				System.out.println("Numbers are :- "+k+" "+arr[i]);
			}
			else
			{
				ht.put(arr[i], 1);
			}
		}
		
	}
Override the hashcode & equals method

- Vir Pratap Uttam May 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why not use a HashSet<Integer> instead of HashMap<Integer, Integer>?

- zortlord May 19, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I will use a HashMap to match a pair of two numbers such that sum is K.
Iterate the unsorted array and for each number a, calculate b = k - a
If number b exists in the map, pair up those a and b and return the pair
otherwise put number a into the map and go on to next number in the array.
Once you go through the array, you can find all pairs that sum up to K.

- xenomacx May 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

def solve(array, K):
    for indexn, n  in enumerate(array):
        for indexm, m  in enumerate(array):
            if indexm != indexn and (n+m) == K:
                print n,m

solve([1,2,3,4,5,6,7], 8)

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

I used two indices, s and e after sorting. Time complexity is O(n * log n). Space complexity is O(1).

static void printSum(int[] arr, int K) {
	Arrays.sort(arr);
	
	int s = 0;
	int e = arr.length - 1;
	while (s < e) {
		if (arr[s] + arr[e] == K) {
			System.out.println(arr[s] + ", " + arr[e]);
			s++;
		} else if (arr[s] + arr[e] > K) {
			e--;
		} else {
			s++;
		}
	}
}

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

Runtime O(n), Memory O(n)

public static int[] getSumPair(int[] arr, int sum){
    HashSet<Integer> contains = new HashSet<Integer>();
    for(int i : arr){
        int needed = sum - i;
        if(contains.contains(needed)){
            return new int[]{i, needed};
        }
        contained.add(i);
    }
    return null;
}

- zortlord May 19, 2015 | 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