Expedia Interview Question for Software Engineer / Developers


Country: India




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

using Hash table is not right approach when we have million of numbers,.,,,,,think

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

First solution is to create a hash table of Integer size. 0- 2pow 32 and initialize it by 0, whenever you get any no. check the value at that position, if it is 0, it is only the first time we have encountered that no. increment it and proceed.

This would have the Time Complexity as O(1) but has space contraints.

Implement a better way to make it Space efficient as well.

- abcd_win September 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the time complexity with solution is O(n) because you have to traverse the entire array to print out all the duplicates. Space is your bitvector size.

- JustCoding September 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes you are correct. Time Complexity is O(n).

- pradegup September 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ohh... I missed this.
I mean you would be fed an stream of numbers(which goes in million) and with every no. you get you have to print if the no. is actually a duplicate.


Sorry My Bad.

- abcd_win September 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if we take two arrays the other array would be of size of the maximum number in the first array +1 as soon as a number is encountered the location corresponding to this element in the second array is incremented.

I hope i am clear

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

@Anonymous... that sounds similar to using a map/hashset... is it?

- JustCoding September 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can use a 1,000,000 bits to do this as well, same approach but use 8 times fewer memory.

Can we modify the content of the array? (i.e. binary sort it then print duplicated from a sorted array)

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

we can do it like this,

int nLength = array.Length();
for ( int nIndex = 0; nIndex < nLength; nIndex++ ) {
while ( arr[i] != i && arr[i] != arr[arr[i]] ) {
swap(arr[i], arr[arr[i]] );
}
}

for ( int nIndex = 0; nIndex < nLength; nIndex++ ) {
if ( arr[i] != i )
print(arr[i]);
}

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

@Manish: this solution assumes the numbers are in a range I think? say the array is of size 10.. and the elements are 1 3 5 7 9 20 18 35 40 100...

arr[arr[i]] will be arr[100] for the last element... and then you'll get out of bounds since your arr size will be 10 and not 100.. right?

- JustCoding October 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Its obvious, that we need to go through all numbers atleast once to find out the duplicates.
If extra space is ok, use hashsets. they store unique elements:
int array[] = { 1, 2, 3, 6, 4, 1, 7, 8, 9, 3, 2, 2, 5, 3, 4, 3, 6, 4 };
Set<Integer> uniqueNums = new HashSet<Integer>(array.length);
Set<Integer> duplicates = new HashSet<Integer>();
for (int i = 0; i < array.length; i++)
{
if (uniqueNums.contains(array[i]))
{
//numbers can be duplicate more then twice, so store unique again
duplicates.add(array[i]);
}
else
{
uniqueNums.add(array[i]);
}
}
System.out.println("Duplicates: " + duplicates);

If no extraspace, then have to compromise of time. Sorting and finding out is the only key

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

This code Works perfectly. Did u take care of running time and space complexity?

- samotgun January 15, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You need not to use contains as purpose of contains can be fulfilled by add operation as it will return false for duplicates and interviewer is only asking for printing the duplicates so you need not to store the duplicates in another hashset( assuming that we need not to worry about the duplicates of duplicates here.)

- amit deol February 17, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if the numbers are in a range then we can use

boolean

array to find out the duplicates. Use numbers as the index of boolean array and mark as you traverse array.
If

boolean

array element is already

true

it is a duplicate.

- rudra January 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

How about this? Please let me know your views on this.
Implement Radix sort & then in linear time we can find the duplicates.

- omgpop November 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I solved it like this

public static void printDuplicates( int[] numbers ) {
		
		Arrays.sort( numbers );
	
		Set<Integer> set = new TreeSet<Integer>();
		for( int i=0; i<numbers.length-1; i++ ) {
			if( numbers[i+1] == numbers[i]) set.add(numbers[i]);
		}
		
		for( Integer i : set)
			System.out.println(i);
	}

- mc March 05, 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