Flipkart Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

Dynamic programming solution:

If I have 1,...i houses, Maximum sum that I can get is represented by recurrence relation:

OPT(i) = max(OPT(i-1), Pi + OPT(i - 2));
where Pi is amount you can get from house i.

- AP May 31, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Try this: 1,8,3,4,10 . It proves ur expression wrong.

dp[i] = max(dp[i-1], v[i] + dp[i-2], v[i] + dp[i-3])

- krgaurav22 January 08, 2016 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

package com.dynamic;

public class MaximumRob {

	public int getMaximum(int[] arr) {
		int excl = 0;
		int incl = arr[0];
		int newExcl;
		for (int i = 1; i < arr.length; i++) {
			newExcl = (excl > incl) ? excl : incl;
			incl = excl + arr[i];
			excl = newExcl;
		}

		return (excl > incl) ? excl : incl;
	}

	public static void main(String args[]) {

		int[] arr = { 10, 2, 3, 5, 7, 8 };

		System.out.println(" Maximum sum is "
				+ new MaximumRob().getMaximum(arr));
	}
}

- akkhil2012 December 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

could you explain the logic, it is quite tricky.

- zr.roman January 08, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

c# implementation.
via peak finding.
Running time 3*n.
O(n) complexity.

private static int GetViaPeakFinding( int[] arr ) {

    int res = 0;
    int prevPeak = -2;

    for ( int i = 0; i < arr.Length; i++ ) {

        if ( ( i == 0 || arr[ i ] > arr[ i - 1 ] ) && ( i == arr.Length - 1 || arr[ i ] > arr[ i + 1 ] ) ) {
            res += arr[ i ];

            var tmpSum1 = 0; var tmpSum2 = 0;
            for ( int j = prevPeak + 2, k = i - 2; ( ( j < i - 1 ) && ( k > prevPeak + 1 ) ); j += 2, k -= 2 ) {
                tmpSum1 += j < i - 1 ? arr[ j ] : 0;
                tmpSum2 += k > prevPeak + 1 ? arr[ k ] : 0;
            }

            res += tmpSum1 > tmpSum2 ? tmpSum1 : tmpSum2;
            prevPeak = i;
            continue;
        }
        if ( i == arr.Length - 1 && arr[ i ] <= arr[ i - 1 ] ) {
            for ( int j = prevPeak + 2; j < arr.Length; j += 2 ) {
                res += arr[ j ];
            }
        }
    }
    return res;
}

- zr.roman January 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

form two arrays :
1:containing the elements at even positions in the given array
2:containing the elements at odd positions in the given array

the array which gives the largest sum

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

This is a greedy strategy and won't necessarily work.
Consider following array:
10 2 3 5 7 8
According to your logic array1: 2 + 5 + 8 = 15 array2: 10 +3 + 7 = 20.
So you will select 20
Optimal solution is 23 by selecting 10 + 5 + 8 = 23, positions 0, 3, 5 no one is neighbor.

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

form two arrays:
1:containing the values at even positions in org. array
2:containing the values at odd positions in org. array

the array with the highest sum

- ne0n May 30, 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