Myntra Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

dp

At i=0;
sum[0]=a[i];
sum[1]=max(a[0],a[1])
sum[i]=max(sum[i-2]+a[i],sum[i-1])
return sum[|a|-1)]

- Anonymous October 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome answer!!

- mitruka.abhishek October 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

I went ahead and just implemented the simplest answer and here it is

ret[0] = a[0];
		ret[1] = a[1];

		for (int i = 2; i < a.length; i++){
			if (ret[0] < a[i] && ret[1] > ret[0])
				ret[0] = a[i];
			else if (ret[1] < a[i])
				ret[1] = a[i];
		}

if line has ret[1] > ret[0] so ret[0] doesn't get changed when ret[1] is smaller than ret[0]

- idk October 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

int[] A = {10, 1, 3, 25, 2, 4, 20}; should indeed result in 55 {10, 25, 20} and so the algo is perfectly correct! What is your point?

- Anon October 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

To solve these type of question, first thing is to find a recurring relation. In this case our recurring relation will tell the max sum till a given length. It means that we will get the max sum for running length of the array. If we denote the ith element as T(i) and max sum till ith element of the array as S(i), then


S(i) = MAX {S(i-1), S(i-2) + T(i) }

S(-1) = 0;
if i=0, S(i) = T(0);

Note: while developing this relationship, I assume array index is starting from 0.

Writing code for this problem is not a big deal now. Below is the code snippet for the same.

sum2 = 0;
sum = sum1 = array[0];

for(i=1; i<len; i++)
{
   sum = MAX(sum2 + array[i], sum1);
   sum2 = sum1;
   sum1 = sum;
}

For a detailed explanation, visit bit.ly/ReP5zl

- Anonymous October 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thank you Anonymous, it's working fine.

- Veeru October 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Try this

for (int i=0;i<a.length-2;i++)
{
for(int j=i+2;j<a.length;j++)
{
sum = a[i]+a[j];
if (temp<sum)
{
temp = sum;
first = a[i];
second = a[j];
}
}
}

- Anonymous October 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>

using namespace std;

main()
{


int SIZE=3;
int arr[]= {10,1,3,25};



int max_sum=0;

for(int i=0; i<SIZE-1; i++)
{
int sum=arr[i]+arr[i+2];
if(sum > max_sum)
{

max_sum=sum;

}



}

int sum1=arr[0] + arr[SIZE];
if(max_sum<sum1)
{

max_sum=sum1;
}
cout<<max_sum<<"is greatest";

}

- nirjhar18 November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import collections
'''
Problem :
Find the subsequences whose elements should not be adjacent and their sum should be maximum from the given array (contains only positive integers).
Eg: int[] A = {10, 1, 3, 25}
Sol: Sum: {10, 3} = 13
{1,25} = 26
{10,25} = 35
Here the Maximum subsequence is {10, 25}.

Approch :

compare the adj elements and find the max and keep them in the sum array
repeat until it's length is 2 (to form a pair)
'''
def findMaxNonAdj(array):
    sumList = []
    temp = array
    if len(array) < 2:
        print "Cannot form a pair"
        return
    while temp:
        for i in range(1,len(temp)):            
            sumList.append(max(temp[i-1],temp[i]))            
        temp = None
        temp = collections.OrderedDict.fromkeys(sumList).keys()#normal sets are unorderd
        print temp
        sumList = list()
        if len(temp) == 2:
            break

    print "Result "+str(temp)

def main():
    nums = [10,1,3,25]
    findMaxNonAdj(nums)

if __name__ == "__main__":main()

- Aditya Pn April 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple and short implementation of Dynamic Programming idea mentioned here :

int sum_max_dp(int arr[], int n){
	int result[n];
	result[0]=arr[0];
	result[1]=max(arr[0],arr[1]);
	for(int i=2;i<n;i++) result[i]= max(result[i-2]+arr[i], result[i-1]);
	return result[n-1];
}

- Perceptron May 28, 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