Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
0
of 2 vote

#include<iostream>

using namespace std;

int main()
{
   int arr[] = {3, 1, 4, 4, 5, 2}; 
   int repeat = 0;
   for(int i = 0; i <= 5 ; i++)
   repeat = repeat^arr[i]^i;

  cout<<repeat;

}
Output:- 4

- Satveer Singh June 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

your code doesn't work for this input {3, 1, 4, 10, 5, 10}.

- shahidul2k9 September 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

325

- faizan October 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

is there constraint on time complexity..?

1. Sort the array
2. Compare current element with next element, if they are same break the loop.

- Dirish Kumar June 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is there constraint on time complexity..?

1. Sort the array
2. Compare current element with next element, if they are same break the loop.

- Dirish Kumar June 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

IF EXTRA SPACE is ALLOWED

use hashSet in java .

1. create a new hashSet HS
2. for every element in the array, check if hashSet contains the element (using HS.contains(element)) .
if the method returns False , then add the element in the hashSet .
3. Traverse next element in arrray and repeat step 2.

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

- Vezita June 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

IF EXTRA SPACE IS NOT ALLOWED .
IF array elements are from 1 to N

Duplicate element = (Sum of array of elements) - (Sum of numbers from 1 to n)

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

- Vezita June 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We need to understand the question better. What is the significance of n=5? Does it mean that it will always have numbers from 1 to 5? if so, then sum of all digits from 1 to 5 = n(n+1)/2 = 15. And sum of the array is 19... 19-15 = 4.

- kranthi July 15, 2014 | 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