Google Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

Use Hash Map...
Say unique number of elements is k
Extra Memory O(k)
Time O(n)

Sort numbers and do it
Time to sort O(n log n)
No Extra memory required

- Amit Modi October 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can also do it with a bit vector to use less memory than a hashmap, while keeping it O(n)

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

How do you do it with bet vector? I see this idea here occasionally and am not familiar with that approach.

- wealthychef June 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Good Question..
Lemme give my view.. Every prgm either hv to compromise of Time or Space Complexity..

Algo:-
1> Space Compromise :-
a> Read a array and hash it.
b> Before putting into hash table check if that place is filled.
c> if Filled its duplicate else not keep it in there.

2> Time Compromise :-

Usual Sorting algo with little modification will work.
a> Pick an element search for its duplicity in the array with min complexity.

- hprem991 November 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is there any time constraint? If not, simply sort tha array and then look for the repeated elements -- O(n.log n).

- pablo.barrientos October 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how about constructing a heap out of this array and when inserting (shuffling) array items if you find what you have in hand is repeated item print it and continue in the same manor. heap construction takes o(n) so will this ...no need to use extra memory use the one array has...

- Anonymous November 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Sort the array in nlogn time (in place). Find Duplicates in O(n) time. Overall 0(n lg n) time. -- Limited memory approach
2. Use Hash table to check if a number is already present. -- Unlimited memory approach.
3. Use distributed approach, lets say you have 10 machines, distribute the numbers as number % 10. And those 10 machines removes the duplicate among assigned number, Union of all those will be the distinguish numbers.
4. Another unlimited memory approach could be BloomFilter (conceptually similar to HashTable). But it save memory if the range of numbers is not too big with respect to the number of numbers. For 32 bit numbers. Memory requirement will be 4BG/8 = 256MB.

- Anonymous November 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

1. Merge sort
2. Heap sort

both O(n log n), heap sort requires no extra memory for array

- jkunzvt October 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

In fact you can get it in O(n)
Maintain two numbers x and y and a temporary variable
while iterating through the array, temp= x^array[i]
if(temp==0), print that number.. else keep x=x^array[i] as it is
in order to see that the same number not to get printed store the printed number in y
temp = y^(printed number)
print the number only if temp!=0
Help this explains the algorithm!!

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

wtf?

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

these are 'exor' operations.. is there anything wrong in the solution?

- SecretAgent October 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sunil: Could you take a small example to express the algo?

- mani001 October 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i am sorry... the above program gives wrong solution in some inputs.. i just realized :)

- SecretAgent November 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your solution works for the following question "we are given a SORTED array, which consists of nos. which occur EVEN no. of times, except one, which occurs ODD no. of times. find the that number"

- Rayden November 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yup.. that's right :)

- SecretAgent November 02, 2011 | Flag


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