Myntra Interview Question for Software Engineer / Developers


Country: -
Interview Type: Phone Interview




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

(1) find max, min in array.
(2) if (max - min + 1 != n) return false;
(3) else do the following:
(a) subtract whole of the array with min value.
(b) for (i = 0; i < n; i++)
{
if(a[a[i]] < 0)
{
print false;
break;
}
else
a[a[i]] *= -1;
}
if ( i == n )
print true;
(c) restore the array i.e make all the elements positive by applying abs operator and then add min to each and every element.

- Anonymous September 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice thinking here.
A novel O(n) time and O(1) space solution unless interviewer have no objection temporary modification of original array like this.

- Montijack January 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

2,2,2,3,2,2,8

max=

- papu8 May 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

2,2,2,3,2,2,8
max=8, min=2, n=7

this will fail in the logic above.

- papu May 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This "logic" will not fail, unless you understood the algorithm used correctly. But yes, we need to take care of situation where subtracting the whole array with min value, gives more than one zero entries since multiplying '-1' to zero won't make it -ve.

- Manish May 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

can we do better than linear?

- Anonymous September 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Of course not.

- Anonymous September 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

One thing not clearly mentioned in the problem is that array is not sorted, so doing it in O(n) would also be tricky :) as you can sort and do it in O(nlogn) times.

- NOBUG September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(n) is possible.

Say given array is A[1...n].

Find max, find min. Check if max - min = n-1.

If max - min = n-1, then we can now have a boolean array of size n, where we check off each number in the interval [min, max].

- Anonymous September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I had given the same answer and interviewer replied "Are you sure this gonna work for all cases?"

- anshulzunke September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Barring overflows, sure it will.

- Anonymous September 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

How will u find duplicacy?

- Anshul Zunke September 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I gave 2 solutions

1. sort the array
2. use another array of booleans and then check uniqueness of each number but space complexity O(N)
3. i thought of it but could nt complete. Using the arithemetic progression formulas

- anshulzunke September 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

So you are looking for an O(1) space solution?

- Anonymous September 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes.
Though Sorting is 1 solution that clicked me instantly but interviewer rejected the obvious sol.

- anshulzunke September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not just traverse the array and find the product of all the elements. Then if the number of elements is n, the product should be equal to n!

- anon September 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

oops this will not work

- anon September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Multiply and Addition approach will not work anyway.

- hemant September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

find the min element and sum of all the elements in the array. Then use AM formula where a=min element and n=arraysize and d=1.

- anonymous September 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Are you sure this will work??

- anshulzunke September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i hope so, can you provide any case where it fails?

- anonymous September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

1 2 3 4 5
1 3 3 3 5

- anshulzunke September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

To put it in a better way:

1) Find the min element and sum of all the elements in the array.
2) Then use Arithmetic series formula where a=min element and n=arraysize and d=1.

Formula: Sn= n/2[2a+(n-1)d]

3) Compare the sum(from step 1) with the above result. If its the same then the numbers are consecutive.

I hope this is right.

- Ady September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It wont work.
I had given u an example. Both the series have same Sum and Min Value but 1 is consecutive and other is not

- anshulzunke September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. You are right.

Then how about adding Step 4 where we check for uniqueness of elements in the array - O(n)

- Ady September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

BTW i had given the exact same answer and both Interviewer and i knew it wasnt the right answer. Infact i had gone a step further by finding the Max and compared it with the max coming in the series

Max = min + (n - 1)* 1



The approach is surely right but i dont think answer will be correct

- anshulzunke September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Uniqueness is the key here. If you get a solution for checking uniqueness of N elements between max and min then u have done ur job.

For the uniqueness only i had suggested having another array but again space complexity would be O(N)

- anshulzunke September 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

True. But NO solution exists which is O(n) time and O(1) space complexity to test uniqueness. Look up in Wikipedia.

- lol September 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

In general, uniquess test takes theta(nlogn). For integer data type, O(n) solution is available.

Read this: en.wikipedia.org/wiki/Element_distinctness_problem
O(n) sol: stackoverflow.com/questions/177118/algorithm-to-determine-if-array-contains-n-nm

- lol September 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how about using both multiply and addition - like even with non-consecutive input that yields correct sum the product won't be what it should be

- aaa September 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ur solution makes sense may b this could get into problem of overflow if the series is bit long

- anshulzunke September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@aaa: it's not wise to GUESS an answer. Try to convince the interviewer with some formula, atleast.

Above approach doesn't work. Ex, [1, 2, 4, 4, 4, 5, 7, 9, 9]. Product (9! = 362880) and sum (45)

- lol September 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. if n<1 return false
2. go throu array with size of n to find min and sum.
3. return true if sum = n*min+n(n-1)/2; otherwise return false.

- Anonymous September 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

check the above replies it wont work for all cases

- anshulzunke September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. as above mentioned, find min, max. Check max-min = n-1, if it is false, return false, otherwise check step2
2. Map all numbers in the array to 1 to n natural numbers by scanning second time. You have min, then
a[i] = a[i]-min+1;
a[i] = a[i] * a[i];
sum = sum + a[i];

Then user the formula, sum of squares of n natural numbers, result = n(n+1)(2n+1)/2.
If sum and result are same, then you can say the numbers are consecutive.

this solution take space complexity of O(n), time complexity of O(2n) ~ O(n)

- Ravi September 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry for typo. the formula is n(n+1)(2n+1)/6

- Ravi September 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess we had discussed the solution above. The solution will have O(N) space complexity

- anshulzunke September 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I didn't use an extra array in step2, modified the original array. If i am wrong, please explain why my algo taking more than N space.

- Ravi September 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry i had misinterpreted ur line "Mapping". Yes ur solution seems good

- anshulzunke September 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Taking sum and sum of squares is not guaranteed to work each time. If you are sure it does, please post a proof here.

Once you do the mapping, you are done. Just check if each of 1 to n appears once. Why even bother with the idiotic sum and sum of squares?

- Anonymous September 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Ravi: don't just guess an answer. No interview would get impressed (even ur idea is wrong), unless you atleast attempt to prove it atleast.

Consider the following sequences, all of which have a sum of 28 and a sum of squares of 140:

[1, 2, 3, 4, 5, 6, 7]
[1, 1, 4, 5, 5, 6, 6]
[2, 2, 3, 3, 4, 7, 7]

- lol September 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry if this question is already solved. But here is my answer:
In first pass find min and max.
Check 1: max - min - 1 != n return false;
Check 2: Do a second pass and perform xor operation in following manner:
int temp = 0;
for (int i=0; i<n; i++){
temp = temp ^ i ^ (A[i] - min)
}
Finally, if temp==0 implies all number are different.

Note: All I wanted is to use solution of "How to find a duplicate element in an array of shuffled consecutive integers?"

- Temp September 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wrong.

- Anonymous September 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do you doubt its analogy with "finding duplicate" problem, or there is any doubt that temp can be 0 for other case also...I could not think of any, please give an counter example

- Temp September 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@TEmp:
Your algorithm cannot distinguish between

1,1,1,1,1,2,3,8

and

1,2,3,4,5,6,7,8

- Anonymous September 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

why dont we simply check consecutive and find that they differ by 1 ???????
why are you making so hard

- Anonymous September 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Read previous comments.....numbers are not sorted

- Anonymous September 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

so hilarious... @1st anonymous... NO INTERVIEWER would give such SILLLLLLLLLLLLLLLLLLLLLLLY question... So take better prep b4 going to face real interview.

- anonymous2 September 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well, if they can ask FizzBuzz, this interpretation is not far behind.

- Anonymous September 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

first find the minimum in 0(n) time
next find xor of all elements subtracting min from each element while taking xor.
ans=a[0]
ans=ans ^ (a[i]-min) for i=1 to n.
if ans ==0 return false
else true
time compl : 0(n) and space : 0(1)

- Abhinav September 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think taking xor will not fetch the ans.
so above sol is wrong.
best solution is to have 0(n) extra space to mark integers from 1 to n after subtracting each element from min and then check for duplication

- Abhinav September 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

step1:
find the min, max,length in o(n).
2. iterate thru the given array once again, subtract min from each number and mark the second array(boolean array) with true for that index
for(int i=0; i< arr.length; i++){
    booleanArray[arr[i] - min] = true;
}
3.iterate thru boolean array and see if there is any non true value - if there, then the given array is not consecutive.

- Ranga September 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For consecutive integers sum of numbers must be as below.

n + (n+1) + (n+2) + ... + (n+k-1) = kn + k(k-1)/2


For any other number function must return false. Simple!

- Manish October 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think we can solve the problem in O(n) time but we need another array of size (max-min+1). my method is find max and min from the given array "a". create an int array "check" of size (max-min+1). again scan the given array "a" and increment the value of check[a[i]-min]. if any element of check != 1 then return false. here is my code. ALL CONSTRUCTIVE COMMENTS ARE ALWAYS WELCOME.

public static void main(String [] args){
        int [] a = {2, 2, 3, 3, 4, 7, 7};
	boolean flag = false;
	int min = Integer.MAX_VALUE;
	int max = Integer.MIN_VALUE;
	for(int i=0; i<a.length; i++){
		if(a[i] < min){
			min = a[i];
		}
		if(a[i] > max){
			max = a[i];
		}
	}
	int [] check = new int[max-min+1]; 
	for(int i=0; i<a.length; i++){
		check[a[i]-min]++;
	}
	for(int i=0; i<check.length; i++){
		if(check[i] == 1){
			flag = true;
		}
		else{
			flag = false;
			break;
		}
	}
	if(flag){
		System.out.println("the array is consicutive");
	}
	else{
		System.out.println("the array is not consicutive");
	}
}

- CAFEBABE December 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i have checked some of the test cases given here. but please check the code with some other test cases you may think potential. please find out if there is any error. that will be of great help. thanking you all.......

- CAFEBABE December 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Step-1: Iterate whole array 'Arr' and Find 'MAX', 'MIN' and 'LENGTH'.
Step-2: if( MAX - MIN + 1 != LENGTH) return false;
Step-3: Generate a HashMap. Check if we have a collision with newly inserted value if so return false or else return that value from Arr to HashMap.
Step-4: If Step-3 iterates through all values of array 'Arr' without any collision then return true.

Complexity: Time O(n), Space O(n)

- Montijack January 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

find minimum of array find the sum of elements of array , it should be (n-1)(n-2)/2 - n*min if yes return true else false.

- nk March 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

min = Integer.MIN_VALUE;
for (i=0; i<a.length; i++) {
  if (a[i] < min)
    min = a[i];
}
Boolean flag = new Boolean();
Integer temp = new Integer();
for (i=0; i<a.length; i++) {
  if((a[i]-min) < a.length) {
    if (a[i] != a[a[i]-min] ) {
      flag = TRUE;
    } else flag = FALSE;
  } else {
    System.out.println('False');
    return;
  while ((a[i] != min+i) || flag) {
    temp = a[i];
    a[i] = a[a[i]-min];
    a[a[i]-min] = temp;
  }

Too lazy to finish the code, I guess you got what I am saying.

- Vetcha May 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Keep an array of length n. In this array we will keep track the position of min and max. Initially both will point to starting of array.

Funda is to form a kind of circular array and keep on inserting elements in sorted manner. If element already exists at that position return false.
Insert first element at starting.
Second element if greater than max will be inserted to position (element-max) and max will be updated, else if less than min inserted at at (minPosition - min + element). If position is less than zero, insert from bottom of array to give impression of circular array.

Now for each element insert it into array.

for (i = 1; i < n ; i++ ) {
if (element <= min) {
    if (max - element > n)
	return false;
    //Insert at proper position, if element exists at this position return false;
} else if (element >= max) {
	if (element - min >n)
		return false;
	//Insert at proper position, if element exists at this position return false;
} else {// element lies between max and min, insert at proper position
	//Insert at proper position, if element exists at this position return false;
}

- gmahindra0 November 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Didn't read all solutions here, sorry if it's a repitition.

1) find min O(n)
2)Subtract min from each element O(n)
3)Sum=n(n-1)/2... Sigma(0 to n-1) O(1) :\
4)Traverse array and keep subtracting each element from Sum O(n)
5)if Sum==0 return true else return false
6)Restore array by adding min if needed

- Anamika August 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Find max, say n, Expected sum s1 = n * (n+1) / 2
2. Find min, say m, Expected sum s2 = m *(m+1) /2

3. Sum of elements in the array s3
4. return s3 == s1 - s2;

this'll work right.

- Vib January 12, 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